import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap void main() { auto rd = readln.split.to!(size_t[]), w = rd[0], h = rd[1], n = rd[2]; auto wh = w * h; auto g = new bool[size_t][](wh); foreach (_; 0..n) { auto m = readln.chomp.to!size_t; auto b = readln.split.to!(size_t[]); foreach (i; 0..m) { auto s = b[i], t = b[i+1]; if (s > t) swap(s, t); if (s/w == t/w) { foreach (j; s..t) { g[j][j+1] = true; g[j+1][j] = true; } } else { auto k = s % w; s /= w; t /= w; foreach (j; s..t) { g[j*w+k][(j+1)*w+k] = true; g[(j+1)*w+k][j*w+k] = true; } } } } struct Qitem { size_t v; int len; } auto q = DList!Qitem(Qitem(0, 0)), visited = new bool[](wh); visited[0] = true; while (!q.empty) { auto qi = q.front; q.removeFront(); foreach (v; g[qi.v].byKey) { if (v == wh-1) { writeln(qi.len + 1); return; } if (!visited[v]) { visited[v] = true; q.insertBack(Qitem(v, qi.len + 1)); } } } writeln("Odekakedekinai.."); }