#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
typedef long long ll;


const int d8[8][2] = {
	{0, 1},
	{1, 0},
	{0, -1},
	{-1, 0},
	{1, 1},
	{1, -1},
	{-1, 1},
	{-1, -1}
};

int h, w;

bool isB(int x, int y) {
	return (0 <= x && x < w && 0 <= y && y < h);
}

const int INF = 10000;
int n;
int l[5];
int px[5][100], py[5][100];
bool isBomb[22][22];

int dp[22][22][50][32];
bool used[22][22][50][32];

int solve(int x, int y, int t, int f) {
//	printf("%d %d %d %d\n", x, y, t, f);
	for (int i = 0; i < n; i++) {
		if (!(f & (1<<i))) continue;
		if (l[i] == t) return INF;
	}
	for (int i = 0; i < n; i++) {
		if (!(f & (1<<i))) continue;
		if (px[i][t] != x || py[i][t] != y) continue;
		f &= ~(1<<i);
	}
	if (!f) return t;
	if (used[x][y][t][f]) return dp[x][y][t][f];
	used[x][y][t][f] = true;

	int res = solve(x, y, t+1, f);
	for (int i = 0; i < 8; i++) {
		int yy = y + d8[i][1], xx = x + d8[i][0];
		if (!isB(xx, yy)) continue;
		if (isBomb[yy][xx]) continue;
		res = min(res, solve(xx, yy, t+1, f));
	}
	return dp[x][y][t][f] = res;
}

int main() {
	cin >> w >> h >> n;
	int sx, sy;
	cin >> sx >> sy; sx--; sy--;
	for (int i = 0; i < n; i++) {
		int k;
		cin >> k;
		l[i] = k;
		for (int j = 0; j < k; j++) {
			cin >> px[i][j] >> py[i][j];
			px[i][j]--; py[i][j]--;
		}
		isBomb[py[i][k-1]][px[i][k-1]] = true;
		l[i]--;
	}
	int res = solve(sx, sy, 0, (1<<n)-1);
	if (res == INF) res = -1;
	printf("%d\n", res);
    return 0;
}