//============================================================================
// Name        : ABC166C.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>

int main() {
	int i, n, m, s, a, b, tempah, tempbh, tempa, tempb;
	int rs, ans = 0;
	int view1[1001];
	int view2[1001];
	int view3[1001];
	int view4[1001];
	int view5[1001];
	int view6[1001];
	int view7[1001];
	int view8[1001];
	int view9[1001];
	int view10[1001];
	int rview1[1001];
	int rview2[1001];
	int rview3[1001];
	int rview4[1001];
	int rview5[1001];
	int rview6[1001];
	int rview7[1001];
	int rview8[1001];
	int rview9[1001];
	int rview10[1001];

	scanf("%d %d", &n, &m);
	s = 1;
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view1[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view2[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view3[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view4[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view5[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view6[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view7[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view8[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view9[i]);
			s++;
		}
	}
	for (i = 0; i < 1000; ++i) {
		if (s <= n) {
			scanf("%d", &view10[i]);
			s++;
		}
	}

	for (i = 1; i <= m; ++i) {
		scanf("%d %d", &a, &b);
		a--;
		b--;
		tempa = a / 1000;
		tempb = b / 1000;
		switch (tempa) {
		case 0:
			tempah = view1[a % 1000];
			break;
		case 1:
			tempah = view2[a % 1000];
			break;
		case 2:
			tempah = view3[a % 1000];
			break;
		case 3:
			tempah = view4[a % 1000];
			break;
		case 4:
			tempah = view5[a % 1000];
			break;
		case 5:
			tempah = view6[a % 1000];
			break;
		case 6:
			tempah = view7[a % 1000];
			break;
		case 7:
			tempah = view8[a % 1000];
			break;
		case 8:
			tempah = view9[a % 1000];
			break;
		case 9:
			tempah = view10[a % 1000];
			break;
		default:
			break;
		}
		switch (tempb) {
		case 0:
			tempbh = view1[b % 1000];
			break;
		case 1:
			tempbh = view2[b % 1000];
			break;
		case 2:
			tempbh = view3[b % 1000];
			break;
		case 3:
			tempbh = view4[b % 1000];
			break;
		case 4:
			tempbh = view5[b % 1000];
			break;
		case 5:
			tempbh = view6[b % 1000];
			break;
		case 6:
			tempbh = view7[b % 1000];
			break;
		case 7:
			tempbh = view8[b % 1000];
			break;
		case 8:
			tempbh = view9[b % 1000];
			break;
		case 9:
			tempbh = view10[b % 1000];
			break;
		default:
			break;
		}
		if (tempah < tempbh) {
			rs = a;
		} else {
			rs = b;
		}
		switch (rs / 1000) {
		case 0:
			++rview1[rs % 1000];
			break;
		case 1:
			++rview2[rs % 1000];
			break;
		case 2:
			++rview3[rs % 1000];
			break;
		case 3:
			++rview4[rs % 1000];
			break;
		case 4:
			++rview5[rs % 1000];
			break;
		case 5:
			++rview6[rs % 1000];
			break;
		case 6:
			++rview7[rs % 1000];
			break;
		case 7:
			++rview8[rs % 1000];
			break;
		case 8:
			++rview9[rs % 1000];
			break;
		case 9:
			++rview10[rs % 1000];
			break;
		default:
			break;
		}
	}

	s = 0;
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview1[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview2[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview3[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview4[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview5[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview6[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview7[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview8[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview9[i] == 0) {
			ans++;
		}
		s++;
	}
	for (i = 0; i < 1000; ++i) {
		if (s < n && rview10[i] == 0) {
			ans++;
		}
		s++;
	}
	printf("%d", ans);
	return 0;
}
