#include using namespace std; typedef long long ll; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef pair< int, int > pii; typedef vector< pii > vp; typedef vector< double > vd; typedef vector< vd > vvd; typedef vector< string > vs; template< class T1, class T2 > int upmin( T1 &x, T2 v ){ if( x > v ){ x = v; return 1; } return 0; } template< class T1, class T2 > int upmax( T1 &x, T2 v ){ if( x < v ){ x = v; return 1; } return 0; } const int INF = 0x3f3f3f3f; int W, H, N; vvi pathup; vvi pathright; void init(){ cin >> W >> H >> N; pathup = pathright = vvi( H, vi( W ) ); for( int i = 0; i < N; ++i ){ int M; cin >> M; int u; cin >> u; for( int j = 0; j < M; ++j ){ int v; cin >> v; int ux = u / W, uy = u % W; int vx = v / W, vy = v % W; if( not ( ux <= vx and uy <= vy ) ) swap( ux, vx ), swap( uy, vy ); // cout << ux << " " << uy << " " << vx << " " << vy << endl; if( ux == vx ) ++pathright[ ux ][ uy ], --pathright[ vx ][ vy ]; else ++pathup[ ux ][ uy ], --pathup[ vx ][ vy ]; u = v; } } } void preprocess(){ for( int i = 0; i < H; ++i ) for( int j = 0; j < W; ++j ){ if( i + 1 < H ) pathup[ i + 1 ][ j ] += pathup[ i ][ j ]; if( j + 1 < W ) pathright[ i ][ j + 1 ] += pathright[ i ][ j ]; } vvi vis( H, vi( W ) ); vis[ 0 ][ 0 ] = 1; queue< tuple< int, int, int > > que; que.emplace( 0, 0, 0 ); while( not que.empty() ){ int d, ux, uy; tie( d, ux, uy ) = que.front(); que.pop(); if( ux == H - 1 and uy == W - 1 ) cout << d << endl, exit( 0 ); if( ux + 1 < H and pathup[ ux ][ uy ] and not vis[ ux + 1 ][ uy ] ) que.emplace( d + 1, ux + 1, uy ); if( ux - 1 >= 0 and pathup[ ux - 1 ][ uy ] and not vis[ ux - 1 ][ uy ] ) que.emplace( d + 1, ux - 1, uy ); if( uy + 1 < W and pathright[ ux ][ uy ] and not vis[ ux ][ uy + 1 ] ) que.emplace( d + 1, ux, uy + 1 ); if( uy - 1 >= 0 and pathright[ ux ][ uy - 1 ] and not vis[ ux ][ uy - 1 ] ) que.emplace( d + 1, ux, uy - 1 ); } cout << "Odekakedekinai.." << endl; } void solve(){ } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }