#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s;
const int nmax = 1e5;
const int inf = 1e9;
vector<int> L[nmax + 1];
int d[nmax + 1];
void bfs(int sursa) {
for(int i = 1; i<=n; i++) {
d[i] = inf;
}
d[sursa] = 0;
queue<int> q;
q.push(sursa);
while(!q.empty()) {
int node = q.front();
q.pop();
for(auto vec : L[node]) {
if(d[vec] > d[node] + 1) {
d[vec] = d[node] + 1;
q.push(vec);
}
}
}
}
int main() {
fin >> n >> m >> s;
for(int i=1; i<=m; i++) {
int x, y;
fin >> x >> y;
L[x].push_back(y);
}
bfs(s);
for(int i=1; i<=n; i++) {
if(d[i] == inf) {
d[i] = -1;
}
fout << d[i] << " ";
}
}