#include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long int D[1000][1000]; int edge[1000][1000][4]; int edge2[1000][1000]; int main( void ) { FILE *in = fopen( "wire.in", "r" ); int T; for( int C = 0;; C ++ ){ int N, S; fscanf( in, "%d%d", &N, &S ); if( N == 0 && S == 0 ) break; set< int > xs, ys; vector< int > x0s; vector< int > y0s; vector< int > x1s; vector< int > y1s; for( int i = 0; i < N; i ++ ){ int x0, y0, x1, y1; fscanf( in, "%d%d%d%d", &x0, &y0, &x1, &y1 ); xs.insert( x0 ); xs.insert( x0 + 1 ); xs.insert( x0 - 1 ); xs.insert( x1 ); xs.insert( x1 + 1 ); xs.insert( x1 - 1 ); ys.insert( y0 ); ys.insert( y0 + 1 ); ys.insert( y0 - 1 ); ys.insert( y1 ); ys.insert( y1 + 1 ); ys.insert( y1 - 1 ); x0s.push_back( x0 ); y0s.push_back( y0 ); x1s.push_back( x1 ); y1s.push_back( y1 ); } xs.insert( 0 ); xs.insert( S - 1 ); ys.insert( 0 ); ys.insert( S - 1 ); int sx, sy, ex, ey; fscanf( in, "%d%d%d%d", &sx, &sy, &ex, &ey ); xs.insert( sx ); ys.insert( sy ); xs.insert( ex ); ys.insert( ey ); map< int, int > xmap, ymap; int X = 0, Y = 0; for( set::iterator it = xs.begin(); it != xs.end(); it ++ ){ if( 0 <= *it && *it < S ) xmap[*it] = X ++; } for( set::iterator it = ys.begin(); it != ys.end(); it ++ ){ if( 0 <= *it && *it < S ) ymap[*it] = Y ++; } sx = xmap[sx]; sy = ymap[sy]; ex = xmap[ex]; ey = ymap[ey]; for( int i = 0; i < N; i ++ ){ x0s[i] = xmap[x0s[i]]; y0s[i] = ymap[y0s[i]]; x1s[i] = xmap[x1s[i]]; y1s[i] = ymap[y1s[i]]; } for( int i = 0; i < X; i ++ ){ for( int j = 0; j < Y; j ++ ){ D[i][j] = -1; edge[i][j][0] = 0; edge[i][j][1] = 0; edge[i][j][2] = 0; edge[i][j][3] = 0; edge2[i][j] = 0; }} for( int i = 0; i < N; i ++ ){ int y0 = min( y0s[i], y1s[i] ); int y1 = max( y0s[i], y1s[i] ); int x0 = min( x0s[i], x1s[i] ); int x1 = max( x0s[i], x1s[i] ); if( x0 == x1 ){ for( int i = y0; i < y1; i ++ ){ edge[x1][i][1] = 1; edge[x1][i+1][3] = 1; edge2[x1][i] = 1; edge2[x1][i+1] = 1; } } else{ for( int i = x0; i < x1; i ++ ){ edge[i] [y1][0] = 1; edge[i+1][y1][2] = 1; edge2[i] [y1] = 1; edge2[i+1][y1] = 1; } } } priority_queue< pair > > wl; wl.push( pair >( -edge2[sx][sy], pair(sx,sy) ) ); while( !wl.empty() ){ int d = - wl.top().first; int x = wl.top().second.first; int y = wl.top().second.second; wl.pop(); if( D[x][y] >= 0 ) continue; D[x][y] = d; if( x == ex && y == ey ) break; int xx, yy; // 0 xx = x + 1, yy = y + 0; if( edge[x][y][0] == 0 && 0 <= xx && xx < X && 0 <= yy && yy < Y && D[xx][yy] < 0 ){ int d2 = d + edge2[xx][yy]; wl.push( pair >( -d2, pair(xx,yy) ) ); } // 1 xx = x + 0, yy = y + 1; if( edge[x][y][1] == 0 && 0 <= xx && xx < X && 0 <= yy && yy < Y && D[xx][yy] < 0 ){ int d2 = d + edge2[xx][yy]; wl.push( pair >( -d2, pair(xx,yy) ) ); } // 2 xx = x - 1, yy = y + 0; if( edge[x][y][2] == 0 && 0 <= xx && xx < X && 0 <= yy && yy < Y && D[xx][yy] < 0 ){ int d2 = d + edge2[xx][yy]; wl.push( pair >( -d2, pair(xx,yy) ) ); } // 3 xx = x + 0, yy = y - 1; if( edge[x][y][3] == 0 && 0 <= xx && xx < X && 0 <= yy && yy < Y && D[xx][yy] < 0 ){ int d2 = d + edge2[xx][yy]; wl.push( pair >( -d2, pair(xx,yy) ) ); } } printf( "%d\n", D[ex][ey] ); } return 0; }