#include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, x, z, c, x1, z1, x2, z2, t = 2, ans;
vector<pair<long long, long long> > v1, v2;
set<long long> s;
map<long long, long long> mp;
int main() {
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> x >> z;
    v1.push_back(make_pair(x, z));
  }
  for (i = 0; i < m; i++) {
    cin >> x >> z;
    v2.push_back(make_pair(x, z));
  }
  while (t--) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        x1 = v1[i].first;
        z1 = v1[i].second;
        x2 = v2[j].first;
        z2 = v2[j].second;
        set<long long> st;
        st.insert(x1);
        st.insert(z1);
        st.insert(x2);
        st.insert(z2);
        if (st.size() != 3) continue;
        if (x1 == x2) x = x2;
        if (z1 == x2) x = x2;
        if (x1 == z2) x = z2;
        if (z1 == z2) x = z2;
        for (k = j + 1; k < m; k++) {
          x1 = v1[i].first;
          z1 = v1[i].second;
          x2 = v2[k].first;
          z2 = v2[k].second;
          set<long long> st1;
          st1.insert(x1);
          st1.insert(z1);
          st1.insert(x2);
          st1.insert(z2);
          if (st1.size() != 3) continue;
          if (x1 == x2) z = x2;
          if (z1 == x2) z = x2;
          if (x1 == z2) z = z2;
          if (z1 == z2) z = z2;
          if (x != z) {
            cout << -1 << endl;
            return 0;
          }
        }
        s.insert(x);
        ans = x;
      }
    }
    swap(v1, v2);
    swap(n, m);
  }
  if (s.size() == 1)
    cout << ans << endl;
  else
    cout << 0 << endl;
  return 0;
}
