#include <bits/stdc++.h>
using namespace std;
#define INF 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;
typedef pair<int,int> ii;
typedef vector<pair<int,int> > vii;
typedef vector<vector<pair<int,int> > > vvii;
#define all(x) (x).begin(), (x).end()
#define nall(x) (x).rbegin(), (x).rend()
#define tr(x,it) for(auto it = (x).begin();it!=(x).end();++it)
#define ntr(x,it) for(auto it = (x).rbegin();it!=(x).rend();++it)
#define ufy(v) sort(all(v));(v).erase(unique(all((v))),(v).end())
#define sz(a) int((a).size())
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define MP make_pair
#define clr clear
#define rz resize
#define C(a,b) memset(a,b,sizeof(a))
#define ia(a,n) FOR(i,0,n-1)cin>>a[i]
#define ia1(a,n) FOR(i,1,n)cin>>a[i]
#define fpresent(c,x) ((c).find(x) != (c).end()) // set,map
#define present(c,x) (find(all(c),x) != (c).end()) //vector
#define F first
#define S second
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define NFOR(i,a,b) for(int i=a;i>=b;--i)
#define rep(i,n) FOR(i,0,n-1)
#define TCASE int __T;cin>>__T;FOR(Tc,1,__T)
inline int add(int a,int b, int m=INF){a+=b;if(a>=m)a-=m;return a;}
inline int mul(int a,int b, int m=INF){return (int)(((ll)a*(ll)b)%m);}
vii g[910];
int d1[910];
int d2[910];
int ans[150010];
int vis[910];
int son[910];
queue<pair<ii,int> > q;
int main()
{
boost;
int n,m;
while(cin>>n)
{
if(n==0)break;
cin>>m;
FOR(i,0,n-1)g[i].clr();
FOR(i,0,m-1)
{
int x,y;
cin>>x>>y;
g[x-1].pb({y-1,i});
ans[i]=0;
}
FOR(i,0,n-1)
{
FOR(j,0,n-1)d1[j]=d2[j]=INF,vis[j]=-1,son[j]=0;
tr(g[i],it)son[it->F]=1;
q.push(MP(MP(i,0),-1));
d1[0]=0;
while(!q.empty())
{
int now=q.front().F.F;
int d=q.front().F.S;
int par=q.front().S;
q.pop();
if(now==i)
{
if(vis[now]==-1)vis[now]++;
else continue;
}
else if(vis[now]==-1)
{
d1[now]=d;vis[now]=par;
}
else if(vis[now]!=-1 and vis[now]!=INF and vis[now]!=par)
{
d2[now]=d;vis[now]=INF;
}
else continue;
tr(g[now],it)
{
q.push(MP(MP(it->F,d+1),(now==i)?it->F:par));
}
}
tr(g[i],it)
{
if(d2[it->F]!=INF)
ans[it->S]=d2[it->F];
}
}
FOR(i,0,m-1)
cout<<ans[i]<<" ";
cout<<"\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIElORiAxMDAwMDAwMDA3Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgdWxsOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IHZpOwp0eXBlZGVmIHZlY3RvcjxsbD4gdmxsOwp0eXBlZGVmIHZlY3Rvcjx2ZWN0b3I8aW50PiA+IHZ2aTsKdHlwZWRlZiBwYWlyPGludCxpbnQ+IGlpOwp0eXBlZGVmIHZlY3RvcjxwYWlyPGludCxpbnQ+ID4gdmlpOwp0eXBlZGVmIHZlY3Rvcjx2ZWN0b3I8cGFpcjxpbnQsaW50PiA+ID4gdnZpaTsKCiNkZWZpbmUgYWxsKHgpICh4KS5iZWdpbigpLCAoeCkuZW5kKCkKI2RlZmluZSBuYWxsKHgpICh4KS5yYmVnaW4oKSwgKHgpLnJlbmQoKQojZGVmaW5lIHRyKHgsaXQpIGZvcihhdXRvIGl0ID0gKHgpLmJlZ2luKCk7aXQhPSh4KS5lbmQoKTsrK2l0KQojZGVmaW5lIG50cih4LGl0KSBmb3IoYXV0byBpdCA9ICh4KS5yYmVnaW4oKTtpdCE9KHgpLnJlbmQoKTsrK2l0KQojZGVmaW5lIHV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UlORil7cmV0dXJuIChpbnQpKCgobGwpYSoobGwpYiklbSk7fQoKCgp2aWkgZ1s5MTBdOwppbnQgZDFbOTEwXTsKaW50IGQyWzkxMF07CmludCBhbnNbMTUwMDEwXTsKaW50IHZpc1s5MTBdOwppbnQgc29uWzkxMF07CnF1ZXVlPHBhaXI8aWksaW50PiA+IHE7CmludCBtYWluKCkKewogICAgYm9vc3Q7CiAgICBpbnQgbixtOwogICAgd2hpbGUoY2luPj5uKSAgICAKICAgIHsKICAgICAgICBpZihuPT0wKWJyZWFrOwogICAgICAgIGNpbj4+bTsKICAgICAgICBGT1IoaSwwLG4tMSlnW2ldLmNscigpOwogICAgICAgIEZPUihpLDAsbS0xKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHgseTsKICAgICAgICAgICAgY2luPj54Pj55OwogICAgICAgICAgICBnW3gtMV0ucGIoe3ktMSxpfSk7CiAgICAgICAgICAgIGFuc1tpXT0wOwogICAgICAgIH0KICAgICAgICBGT1IoaSwwLG4tMSkKICAgICAgICB7CiAgICAgICAgICAgIEZPUihqLDAsbi0xKWQxW2pdPWQyW2pdPUlORix2aXNbal09LTEsc29uW2pdPTA7CgogICAgICAgICAgICB0cihnW2ldLGl0KXNvbltpdC0+Rl09MTsKCiAgICAgICAgICAgIHEucHVzaChNUChNUChpLDApLC0xKSk7CiAgICAgICAgICAgIGQxWzBdPTA7CiAgICAgICAgICAgIHdoaWxlKCFxLmVtcHR5KCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludCBub3c9cS5mcm9udCgpLkYuRjsKICAgICAgICAgICAgICAgIGludCBkPXEuZnJvbnQoKS5GLlM7CiAgICAgICAgICAgICAgICBpbnQgcGFyPXEuZnJvbnQoKS5TOwogICAgICAgICAgICAgICAgcS5wb3AoKTsKICAgICAgICAgICAgICAgIGlmKG5vdz09aSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZih2aXNbbm93XT09LTEpdmlzW25vd10rKzsKICAgICAgICAgICAgICAgICAgICBlbHNlIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBlbHNlIGlmKHZpc1tub3ddPT0tMSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBkMVtub3ddPWQ7dmlzW25vd109cGFyOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZih2aXNbbm93XSE9LTEgYW5kIHZpc1tub3ddIT1JTkYgYW5kIHZpc1tub3ddIT1wYXIpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZDJbbm93XT1kO3Zpc1tub3ddPUlORjsgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlIGNvbnRpbnVlOwoKICAgICAgICAgICAgICAgIHRyKGdbbm93XSxpdCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBxLnB1c2goTVAoTVAoaXQtPkYsZCsxKSwobm93PT1pKT9pdC0+RjpwYXIpKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICB0cihnW2ldLGl0KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZihkMltpdC0+Rl0hPUlORikKICAgICAgICAgICAgICAgIGFuc1tpdC0+U109ZDJbaXQtPkZdOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAgICAgRk9SKGksMCxtLTEpCiAgICAgICAgICAgIGNvdXQ8PGFuc1tpXTw8IiAiOwogICAgICAgICAgICBjb3V0PDwiXG4iOwogICAgfQogICAgcmV0dXJuIDA7Cn0=