#include <fstream>
#include <unordered_map>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<vector<int>> graph;
int N,M,S;
void bfs(int start){
queue<int> varfuri;
int viz[100001] = {0};
vector<int> dist(N+1, -1);
varfuri.push(start);
viz[start] = 1;
dist[start] = 0;
while(!varfuri.empty()){
int ultimNod = varfuri.front();
varfuri.pop();
//af
for(int i = 0; i < graph[ultimNod].size();i++){
int nodVecin = graph[ultimNod][i];
if(viz[nodVecin] == 0){
varfuri.push(nodVecin);
viz[nodVecin] = 1;
dist[nodVecin] = dist[ultimNod]+1;
}
}
}
for(int i = 1; i<dist.size();++i){
fout << dist[i] << ' ';
}
}
int main()
{
fin >> N >> M >> S;
graph.resize(N+1);
for(int i = 0; i < M; ++i){
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
bfs(S);
return 0;
}