#include int w, h, n, WE[999][1000], NS[1000][999]; int val[1000][1000], queue_x[1000], queue_y[100], set, get; int main(void) { scanf("%d%d%d", &w, &h, &n); int i, j; for(i = 0; i < n; i++) { int m, b, p; scanf("%d%d", &m, &b); for(j = 0; j < m; j++) { p = b; scanf("%d", &b); if(b % w == p % w) { int x = b % w; int y_s = (p < b ? p : b) / w; int y_g = (p < b ? b : p) / w; int k; for(k = y_s; k < y_g; k++) { NS[x][k] = 1; } } else { int x_s = (p < b ? p : b) % w; int x_g = (p < b ? b : p) % w; int y = b / w; int k; for(k = x_s; k < x_g; k++) { WE[k][y] = 1; } } } } int dx[4] = { 1, 0, -1, 0 }; int dy[4] = { 0, 1, 0, -1 }; val[0][0] = 1; queue_x[set] = 0; queue_y[set] = 0; set++; while(set != get) { int x = queue_x[get]; int y = queue_y[get]; get++; for(i = 0; i < 4; i++) { int x2 = x + dx[i]; int y2 = y + dy[i]; if( ( ( i % 2 == 0 && 0 <= x2 && x2 < w && WE[x - (i == 2)][y] ) || ( i % 2 == 1 && 0 <= y2 && y2 < h && NS[x][y - (i == 3)] ) ) && val[x2][y2] == 0 ) { val[x2][y2] = val[x][y] + 1; queue_x[set] = x2; queue_y[set] = y2; set++; } } } if(val[w - 1][h - 1] != 0) { printf("%d\n", val[w - 1][h - 1] - 1); } else { printf("Odekakedekinai..\n"); } return 0; }