#include <bits/stdc++.h>
using namespace std;
pair<int, int> p[15][3];
set<int> c[15][3];
set<int> s;
int func(int a, int b, int c, int d) {
  if ((a == c && b == d) || (a == d && b == c)) {
    return -1;
  } else if (a != c && a != d && b != c && b != d) {
    return -1;
  } else {
    if (a == c || a == d)
      return a;
    else
      return b;
  }
}
int main() {
  int n, m, x;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> p[i][0].first >> p[i][0].second;
  }
  for (int i = 0; i < m; ++i) {
    cin >> p[i][1].first >> p[i][1].second;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      x = func(p[i][0].first, p[i][0].second, p[j][1].first, p[j][1].second);
      if (x != -1) {
        s.insert(x);
        c[i][1].insert(x);
        c[j][2].insert(x);
      }
    }
  }
  if (s.size() == 1)
    cout << *(s.begin());
  else {
    for (int i = 0; i < n; ++i) {
      if (c[i][1].size() > 1) {
        cout << -1;
        return 0;
      }
    }
    for (int j = 0; j < m; ++j) {
      if (c[j][2].size() > 1) {
        cout << -1;
        return 0;
      }
    }
    cout << 0;
  }
  return 0;
}
