#include <iostream>
using namespace std;
int S[1000];
int t=-1;
int m[100][2];
void push(int x){
t++;
S[t]=x;
}
void pop(int &x)
{
x=S[t];
t--;
}
bool DFS(int s)
{
push(s);
//duyet
while(t!=-1){
pop(s);
for(int i= 0;i<2;i++){
if(m[s][i]==99) return true;
if(m[s][i]!=-1){
push(m[s][i]);
m[s][i]=-1;
}
}
}
return false;
}
int main()
{
freopen("input.txt","r",stdin);
for(int tc=1;tc<=10;tc++){
//reset
t=-1;
for(int i=0;i<100;i++){
m[i][0]=m[i][1]=-1;
}
int n; cin >>n>>n;
int s;
for(int i=0;i<n;i++){
cin >> s;
if(m[s][0]==-1) cin >> m[s][0];
else cin >> m[s][1];
}
cout << "#"<<tc<<" " << DFS(0)<<endl;
}
return 0;
}