#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] == INF) {
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] << " ";
}
return 0;
}