#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s;
vector<int> mat[100005];
int sir[100005];
void bfs() {
queue<int> q;
q.push(s);
sir[s] = 1;
while (!q.empty()) {
int p = q.front();
for (int i = 0; i < mat[p].size(); ++i) {
if (sir[mat[p][i]] == 0) {
q.push(mat[p][i]);
sir[mat[p][i]] = sir[p] + 1;
}
}
q.pop();
}
}
int main() {
fin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
mat[x].push_back(y);
}
bfs();
for (int i = 1; i <= n; ++i) {
if (sir[i] == 0) {
fout << -1 << " ";
} else {
fout << sir[i] - 1 << " ";
}
}
return 0;
}