#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
int n, m;
vector<pair<int, int>> p1;
vector<pair<int, int>> p2;
int check(int a1, int b1, int a2, int b2) {
  if (a1 == a2 && b1 != b2) return a1;
  if (a1 == b2 && b1 != a2) return a1;
  if (b1 == a2 && a1 != b2) return b1;
  if (b1 == b2 && a1 != a2) return b1;
  return -1;
}
int main() {
  cin >> n >> m;
  p1.clear();
  p2.clear();
  for (int i = 1; i <= n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    p1.push_back(make_pair(x, y));
  }
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    p2.push_back(make_pair(x, y));
  }
  bool ok = true;
  int cnt = 0;
  set<int> xs;
  xs.clear();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int a1 = p1[i].first;
      int b1 = p1[i].second;
      int a2 = p2[j].first;
      int b2 = p2[j].second;
      int res = check(a1, b1, a2, b2);
      if (res > 0) {
        xs.insert(res);
        int tot = 0;
        for (int k = 0; k < m; ++k) {
          int tmp = check(a1, b1, p2[k].first, p2[k].second);
          if (tmp > 0 && tmp != res) {
            tot++;
          }
        }
        if (tot > 0) {
          ok = false;
          break;
        }
        tot = 0;
        for (int k = 0; k < n; ++k) {
          int tmp = check(p1[k].first, p1[k].second, a2, b2);
          if (tmp > 0 && tmp != res) {
            tot++;
          }
        }
        if (tot > 0) {
          ok = false;
          break;
        }
      }
    }
  }
  if (!ok)
    cout << -1 << endl;
  else {
    if (xs.size() > 1)
      cout << 0 << endl;
    else {
      cout << (*xs.begin()) << endl;
    }
  }
  return 0;
}
