#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <memory.h>

using namespace std;

const int MaxN = 1000100;

int n, n1, n2, d, currPoints;
int c[10];
int a[MaxN][10];
char s[MaxN], p[MaxN];

bool solve(int n, int d, char s[]) {
	int count[10], pos;

	memset(count, 0, sizeof(count));
	for (int i = 0; i < n; i++)
		count[s[i] - 48]++;
	pos = 0;
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < count[i]; j++) 
			p[pos++] = (char)(i + 48);

	memset(a, 0, sizeof(a));
	for (int i = 0; i < n; i++) {
		a[i % d][s[i] - 48]++;
		a[i % d][p[i] - 48]--;
	}

	int sol = 0;
	for (int i = 0; i < d; i++)
		for (int j = 0; j < 10; j++)
			if (a[i][j] > 0) sol += a[i][j];

	return (sol == 0);

}

int main(int argc, char *argv[]){

	ifstream inFile(argv[1]);
	ifstream outFile(argv[2]);
	ifstream solFile(argv[3]);
	
	bool ok = true;
	currPoints = 0;

	//da li je isti rezultat
	solFile >> n1;
	if (!outFile.eof()) {
		outFile >> n2;
		ok = (n1 == n2);
		if (ok) currPoints = 5;
	}
	else
		ok = false;
		
	if (ok) {
		inFile >> n;
		inFile >> d;
		inFile >> s;
		memset(c, 0, sizeof(c));
		int x, y;
		for (int i = 0; i < n1 && ok; i++) 
			// ako postoje x i y
			if (!outFile.eof()) {
				outFile >> x;
				if (!outFile.eof())
					outFile >> y;
				else
					ok = false;
				// ako upadaju u data ogranicenja
				if (ok && 1 <= x && x <= n && 0 <= y && y <= 9) {
					c[ s[x - 1] - 48 ]--;
					c[y]++;
					s[x - 1] = (char)(y + 48);
				}
				else
					ok = false;
			}
			else
				ok = false;
		
		for (int i = 0; i < 10 && ok; i++)
			ok = (c[i] == 0);

		if (ok) ok = solve(n, d, s);
	}

	if (ok) currPoints = 10;

	cout << currPoints;

	inFile.close();
	outFile.close();
	solFile.close();

	return 0;
}