#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<string, int>conv;
int love[200005], indeg[200005], brr=1;
bool matched[200005];
int getid(string s)
{
if(conv[s]==0){conv[s]=brr; brr++;}
return conv[s];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
for(int i=0; i<n; i++)
{
string a, b;
cin>>a>>b;
int u=getid(a);
int v=getid(b);
love[u]=v;
}
if(n%2!=0){cout<<-1<<endl; return 0;}
int cnt=0;
for(int i=1; i<=n; i++)
{
if(!matched[i])
{
if (i!=love[i] && love[love[i]]==i)
{
matched[i]=true;
matched[love[i]]=true;
cnt+=2;
}
}
}
for(int i=1; i<=n; i++)if(!matched[i])indeg[love[i]]++;
queue<int>q;
for(int i=1; i<=n; i++)if(indeg[i]==0 && !matched[i])q.push(i);
while(!q.empty())
{
int u=q.front();
q.pop();
if(matched[u])continue;
int v=love[u];
if(!matched[v])
{
matched[u]=true; matched[v]=true;
cnt+=1;
int t=love[v];
if(!matched[t])
{
indeg[t]--;
if(indeg[t]==0)q.push(t);
}
}
}
for(int i=1; i<=n; i++)
{
if (!matched[i])
{
int curr=i, s=0;
while(!matched[curr])
{
matched[curr]=true;
curr=love[curr];
s++;
}
cnt+=(s/2);
}
}
cout<<n-cnt<<"\n";
return 0;
}