#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n,m,s;
const int N=1e5+1;
vector<vector<int>>g;
vector<int>d;
void citire()
{
fin>>n>>m>>s;
int x,y;
g.resize(n+1);
d.resize(n+1,-1);
for(int i=0; i<m; i++)
{
fin>>x>>y;
g[x].push_back(y);
}
}
void bfs(int nod) {
queue<int>q;
q.push(nod);
d[nod]=0;
while (!q.empty()) {
int x=q.front();
q.pop();
for (auto y: g[x])
if (d[y]==-1){
d[y]=d[x]+1;
q.push(y);
}
}
}
int main()
{
citire();
bfs(s);
for (int i=1;i<=n;i++)
fout<<d[i]<<' ';
return 0;
}