#include <bits/stdc++.h>
using namespace std;
const int MAXI=100001;
int f[MAXI],a,b;
int n,tot;
bool is_circle,p[MAXI];
void fi() {
for(int i=1; i<=MAXI; ++i)f[i]=i,p[i]=false;
is_circle=false;
tot=0;
}
int find(int x) {
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
int main() {
while(scanf("%d%d",&a,&b)) {
if(a==-1&&b==-1)break;
if(a==b&&a==0) {
if(is_circle==false&&tot==1)puts("1");
else puts("0");
fi();
continue;
}
if(!p[a])tot++;
if(!p[b])tot++;
p[a]=p[b]=true;
int a1=find(a),b1=find(b);
if(a1!=b1)f[a1]=b1,tot--;
else is_circle=true;
}
return 0;
}
Rt,全输出0.。。