#include <bits/stdc++.h>   using namespace std;   set<pair<int, int> > s[2];   bool eq(pair<int, int> a, pair<int, int> b) {     return (a.first == b.first && a.second == b.second);   }   int main() {     int n, m, i, a, b, j;     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++) {       scanf( %d%d , &a, &b);       if (b < a) swap(a, b);       s[0].insert(make_pair(a, b));     }     for (i = 0; i < m; i++) {       scanf( %d%d , &a, &b);       if (b < a) swap(a, b);       s[1].insert(make_pair(a, b));     }     set<int> ans;     for (auto i : s[0])       for (auto j : s[1]) {         if (i.first == j.first && i.second == j.second)           continue;         else if (i.first != j.first && i.first != j.second &&                  i.second != j.first && i.second != j.second)           continue;         else if (i.first == j.first) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.second || k.second == j.second)               return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.second || k.second == i.second)               return !printf( -1 n );           }           ans.insert(i.first);         } else if (i.first == j.second) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.first || k.second == j.first) return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.second || k.second == i.second)               return !printf( -1 n );           }           ans.insert(i.first);         } else if (i.second == j.second) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.first || k.second == j.first) return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.first || k.second == i.first) return !printf( -1 n );           }           ans.insert(i.second);         } else if (i.second == j.first) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.second || k.second == j.second)               return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.first || k.second == i.first) return !printf( -1 n );           }           ans.insert(i.second);         }       }     if ((int)ans.size() > 1) return !printf( 0 n );     cout << *ans.begin() << endl;   }   