#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll SIZE = 100100;
bool info[SIZE];
ll cnt_matching(ll node, ll p, ll cnt, bool ignore_cyc, vector<ll> &vis, vector<vector<ll>> &g, vector<bool> &matched){
vis[node] = cnt;
ll ans = 0;
for(ll i: g[node]) if((!vis[i] || (ignore_cyc && vis[i] == 1)) && (ignore_cyc || !info[i]) && !matched[i]){
ans += cnt_matching(i, node, cnt, ignore_cyc, vis, g, matched);
if(!matched[i] && !matched[node]) matched[i] = matched[node] = 1, ans++;
}
return ans;
}
ll cnt_for_cyc(vector<ll> &cyc, vector<bool> &matched, ll &pairs){
ll s = 0, n = cyc.size();
for(ll i = 0; i < n; i++) if(matched[cyc[i]]) s = i;
if(!matched[cyc[s]]) {
if(n == 2) {pairs++; return 0;}
return n/2;
}
ll cnt = 0, ans = 0;
for(ll i = (s+1) % n; i != s; i = (i+1) % n){
if(matched[cyc[i]]) ans += cnt/2, cnt = 0;
else cnt++;
}
ans += cnt/2;
return ans;
}
ll solve(ll n, vector<ll> &edge, vector<vector<ll>> &rg){
if(n % 2) return -1;
vector<ll> ind(n+1);
for(ll i = 1; i <= n; i++) ind[edge[i]]++;
queue<ll> q;
for(ll i = 1; i <= n; i++) if(ind[i] == 0) q.push(i);
vector<bool> matched(n+1);
ll change = 0;
while(q.size()){
ll node = q.front(); q.pop();
if(!matched[node]){
if(!matched[edge[node]]) matched[edge[node]] = matched[node] = 1;
change++;
}
ind[edge[node]]--;
if(ind[edge[node]] == 0) q.push(edge[node]);
}
vector<bool> vis(n+1);
for(ll i = 1; i <= n; i++) if(ind[i] && !vis[i]){
ll node = edge[i];
vector<ll> cyc; cyc.push_back(i); vis[i] = 1;
while(node != i) cyc.push_back(node), vis[node] = 1, node = edge[node];
ll idx = -1;
for(ll x = 0; x < cyc.size(); x++) if(matched[cyc[x]]) idx = x;
if(idx == -1){
if(cyc.size() != 2) change += (cyc.size() + 1) / 2;
continue;
}
ll len = 0;
for(ll x = (idx+1) % cyc.size(); x != idx; x = (x+1) % cyc.size()) {
if(matched[cyc[x]]) change += (len+1) / 2;
else len++;
}
change += (len+1) / 2;
}
return change;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll n, cnt = 0; cin >> n;
vector<ll> edge(n+1);
vector<vector<ll>> rg(n+1);
map<string, ll> names;
for(ll i = 0; i < n; i++){
string s, t; cin >> s >> t;
if(!names[s]) names[s] = ++cnt;
if(!names[t]) names[t] = ++cnt;
edge[names[s]] = names[t]; rg[names[t]].push_back(names[s]);
}
cout << solve(n, edge, rg);
}