#include <bits/stdc++.h>
using namespace std;

#define X first
#define Y second

struct NODE{
	int x,y,time,bit;
};
int done[25][25][100][1<<5];
int bomb[25][25];
vector< pair<int,int> > path[5];
int main(){
	int W,H;
	int N;
	cin >> W >> H >> N;
	int sx,sy;
	cin >> sx >> sy;
	for(int i = 0 ; i < 25 ; i++)
		for(int j = 0 ; j < 25 ; j++)
			bomb[i][j] = 1;
	for(int i = 1 ; i <= H ; i++)
		for(int j = 1 ; j <= W ; j++)
			bomb[i][j] = 0;

	
	for(int i = 0 ; i < N ; i++){
		int L;
		cin >> L;
		int x,y;
		while(L--){
			cin >> x >> y;
			path[i].push_back(make_pair(x,y));
		}
		
		bomb[y][x] = 1;
	}
	queue<NODE> Q;
	Q.push({sx,sy,0,0});
	while( Q.size() ){
		NODE q = Q.front(); Q.pop();
		if( done[q.x][q.y][q.time][q.bit]++ ) continue;
		int badflag = 0;
		
		for(int i = 0 ; i < N ; i++){
			if( (q.bit >> i & 1) == 0 && q.time + 1 == path[i].size() ) badflag = 1;
		}
		if(badflag) continue;
		int nextbit = q.bit;
		for(int i = 0 ; i < N ; i++){
			if( (q.bit >> i & 1) == 0 ){
				if( path[i][q.time].X == q.x && path[i][q.time].Y == q.y ) nextbit |= (1<<i);
			}
		}
		if( nextbit == (1<<N)-1 ){
			cout << q.time << endl;
			return 0;
		}
		
		for(int dx = -1 ; dx <= 1 ; dx++){
			for(int dy = -1 ; dy <= 1 ; dy++){
				int tx = q.x + dx;
				int ty = q.y + dy;
				if( bomb[ty][tx] ) continue;
				Q.push({tx,ty,q.time+1,nextbit});
			}
		}
	}
	cout << -1 << endl;
}