// Arup Guha
// 11/26/2024
// Solution to 2024 SER D1/D2 Problem L: Pianissimo

import java.util.*;
import java.io.*;

public class pianissimo {

	final public static String[] dyn = {"ppp","pp","p","mp","mf","f","ff","fff"};
	
	public static void main(String[] args) throws Exception {
	
		// Map for dynamics.
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		for (int i=0; i<dyn.length; i++)
			map.put(dyn[i], i);
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int nNotes = Integer.parseInt(tok.nextToken());
		int nLevels = Integer.parseInt(tok.nextToken());
		
		// Read in notes and get all unique ones.
		TreeSet<Integer> unique = new TreeSet<Integer>();
		int[] notes = new int[nNotes];
		for (int i=0; i<nNotes; i++) {
			notes[i] = Integer.parseInt(stdin.readLine().trim());
			unique.add(notes[i]);
		}
		
		// Map each value to unique ordered integer.
		TreeMap<Integer,Integer> nMap = new TreeMap<Integer,Integer>();
		int idx = 1;
		while (unique.size() > 0) {
			int nxt = unique.pollFirst();
			nMap.put(nxt, idx++);
		}
		
		// Map them.
		for (int i=0; i<nNotes; i++)
			notes[i] = nMap.get(notes[i]);
			
		// Read into parallel lists.
		ArrayList<Integer> nList = new ArrayList<Integer>();
		ArrayList<Integer> mydyn = new ArrayList<Integer>();
		for (int i=0; i<nLevels; i++) {
			tok = new StringTokenizer(stdin.readLine());
			nList.add(Integer.parseInt(tok.nextToken())-1);
			mydyn.add(map.get(tok.nextToken()));
		}
		
		// Store each note dynamic here.
		int[] alldyn = new int[nNotes];
		
		// Copy between all pairs.
		for (int i=0; i<nLevels-1; i++)
			for (int j=nList.get(i); j<nList.get(i+1); j++)
				alldyn[j] = mydyn.get(i);
				
		// Last one goes to the end.
		for (int j=nList.get(nLevels-1); j<nNotes; j++)
			alldyn[j] = mydyn.get(nLevels-1);
			
		// Make bits.
		bit[] mybits = new bit[dyn.length];
		for (int i=0; i<mybits.length; i++)
			mybits[i] = new bit(200100);
		
		long res = 0;
		
		// Go through each note.
		for (int i=0; i<nNotes; i++) {
		
			// Get note and dynamic.
			int curL = notes[i];
			int curD = alldyn[i];
			
			// Go through all bits that store "lower" notes.
			for (int j=0; j<curD; j++)
				res += mybits[j].above(curL-1);
				
			// These are the ones that are supposed to be higher but aren't.
			for (int j=curD+1; j<mybits.length; j++)
				res += mybits[j].atOrBelow(curL);
			
			// Update the appropriate bit with this volume level note.
			mybits[curD].add(curL, 1);
		}
		
		// Ta da!
		System.out.println(res);
	}
	
}

class bit {

	public int[] arr;
	public int n;
	
	public bit(int myn) {
		n = 1;
		while (n <= myn) n = (n<<1);
		arr = new int[n];
	}
	
	// Adds val to index idx.
	public void add(int idx, int val) {
		while (idx < arr.length) {
			arr[idx] += val;
			idx += (idx&(-idx));
		}
	}
	
	// Returns sum from 1..hi.
	public int atOrBelow(int idx) {
		int res = 0;
		while (idx>0) {
			res += arr[idx];
			idx -= (idx&(-idx));
		}
		return res;
	}
	
	// Returns sum above idx.
	public int above(int idx) {
		return atOrBelow(arr.length-1) - atOrBelow(idx);
	}
}