#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  pair<int, int> a[n], b[m];
  int x, y;
  set<int> na[n], nb[m], s;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a[i] = make_pair(x, y);
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    b[i] = make_pair(x, y);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      map<int, int> make_pair;
      make_pair[a[i].first]++;
      make_pair[a[i].second]++;
      make_pair[b[j].first]++;
      make_pair[b[j].second]++;
      if (make_pair.size() == 3) {
        long long x = 0;
        for (auto i : make_pair) {
          if (i.second == 2) {
            x = i.first;
            break;
          }
        }
        na[i].insert(x);
        nb[j].insert(x);
        s.insert(x);
      }
    }
  }
  int flag1 = 0, flag2 = 0;
  for (int i = 0; i < n; i++) {
    if (na[i].size() > 1) {
      flag1 = 1;
      break;
    }
  }
  for (int i = 0; i < m; i++) {
    if (nb[i].size() > 1) {
      flag2 = 1;
      break;
    }
  }
  if (s.size() <= 1) {
    cout << *s.begin();
    return 0;
  }
  if (flag1 + flag2 == 0) {
    cout << 0;
    return 0;
  }
  cout << -1;
  return 0;
}
