#include #include #include #include using namespace std; set>> seen[3], good[3]; bool win(vector>& g, int loc) { if(seen[loc].count(g)) return good[loc].count(g); seen[loc].insert(g); for(int a = 0; a < g[loc].size(); a++) { if(g[loc][a]) { g[loc][a]--; bool ret = !win(g, a); g[loc][a]++; if(ret) { good[loc].insert(g); return true; } } } return false; } bool islost(vector> g, int src) { for(int i = 0; i < g.size(); i++) g[i][i] %= 2; for(int i = 0; i < g.size(); i++) for(int j = 0; j < i; j++) { int x = min(g[i][j], g[j][i]); g[i][j] -= x; g[j][i] -= x; } return !win(g, src); } int main() { int n; cin >> n; vector v(n); vector alpha; for(auto& x: v) { cin >> x; alpha.push_back(x[0]); alpha.push_back(x.back()); } sort(alpha.begin(), alpha.end()); alpha.erase(unique(alpha.begin(), alpha.end()), alpha.end()); vector> g(alpha.size()); for(auto& x: g) x.resize(alpha.size()); for(const auto& x: v) { int lhs = lower_bound(alpha.begin(), alpha.end(), x[0]) - alpha.begin(); int rhs = lower_bound(alpha.begin(), alpha.end(), x.back()) - alpha.begin(); g[lhs][rhs]++; } int ret = 0; for(const auto& x: v) { int lhs = lower_bound(alpha.begin(), alpha.end(), x[0]) - alpha.begin(); int rhs = lower_bound(alpha.begin(), alpha.end(), x.back()) - alpha.begin(); g[lhs][rhs]--; if(islost(g, rhs)) ret++; g[lhs][rhs]++; } cout << ret << "\n"; }