#include <bits/stdc++.h>
using namespace std;
int n, m;
bool valid = 1;
set<int> ans;
pair<int, int> a[12], b[12];
int comval(int x, int y) {
  if (a[x].first == b[y].first) return a[x].first;
  if (a[x].first == b[y].second) return a[x].first;
  if (a[x].second == b[y].first) return a[x].second;
  if (a[x].second == b[y].second) return a[x].second;
  return -1;
}
bool share(int x, int y) {
  return ((comval(x, y) != -1) &&
          a[x].first + a[x].second != b[y].first + b[y].second);
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(20);
  cout.tie(nullptr);
  cin.tie(nullptr);
  ;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
  for (int i = 0; i < m; ++i) cin >> b[i].first >> b[i].second;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (share(i, j)) {
        bool f = 1;
        int c = comval(i, j);
        for (int r = 0; r < n; ++r) {
          if (share(r, j) && comval(r, j) != c && comval(r, j) != -1) {
            f = 0;
          }
        }
        for (int r = 0; r < m; ++r) {
          if (share(i, r) && comval(i, r) != c && comval(i, r) != -1) {
            f = 0;
          }
        }
        if (!f)
          valid = 0;
        else
          ans.insert(c);
      }
    }
  }
  if (valid) {
    if (ans.size() == 1)
      cout << *(ans.begin());
    else
      cout << 0;
  } else
    cout << -1;
}
