#include <bits/stdc++.h>
using namespace std;
const string FILENAME = "bfs";
ifstream f(FILENAME + ".in");
ofstream g(FILENAME + ".out");
#define cin f
#define cout g
int n,m;
int start;
vector<int> gg[100001];
int cst[100001];
void bfs(int start) {
for(int i = 1; i <= n; ++i) {
cst[i] = -1;
}
queue<int> q;
q.push(start);
cst[start] = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
for(int x : gg[node]) {
if(cst[x] == -1) {
q.push(x);
cst[x] = cst[node] + 1;
}
}
}
}
int main() {
cin >> n >> m >> start;
for(int i = 1; i <= m; ++i) {
int x,y;
cin >> x >> y;
gg[x].push_back(y);
}
bfs(start);
for(int i = 1; i <= n; ++i) {
cout << cst[i] << ' ';
}
return 0;
}