#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
#define MAXN 500
#define MAXM 1001
int dist[MAXN][MAXN];
pair<int,int> coord[MAXM];
int N,M;
int sx,sy;
int dx[] = {-2,-2,-1,-1,1,1,2,2};
int dy[] = {-1,1,-2,2,-2,2,-1,1};
queue<pair<int,int> > q;
void bfs(){
q.push({sx,sy});
dist[sx][sy] = 0;
while (!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0;k<8; k++){
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny<0 || nx>=N || ny>=N) continue;
if (dist[nx][ny]!= -1) continue;
dist[nx][ny] = dist[x][y] + 1;
q.push({nx,ny});
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
memset(dist,-1,sizeof(dist));
cin >> N >> M >> sx >> sy;
sx--;
sy--;
for (int i = 1;i<=M;i++){
int x,y;
cin >> x >> y;
x--;
y--;
coord[i] = {x,y};
}
bfs();
for (int i = 1; i <=M; i++){
cout << dist[coord[i].first][coord[i].second]<< " ";
}
return 0;
}