#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool uax(T &x, T y) {
  return (y > x) ? x = y, true : false;
}
template <typename T>
inline bool uin(T &x, T y) {
  return (y < x) ? x = y, true : false;
}
template <typename T>
void kek(T ans) {
  cout << ans << endl;
  exit(0);
}
const int MOD = (int)1e9 + 7;
const long long INF = (long long)1e18 + 42;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<array<int, 2>> a(n), b(m);
  for (auto &x : a) {
    cin >> x[0] >> x[1];
    sort(begin(x), end(x));
  }
  for (auto &x : b) {
    cin >> x[0] >> x[1];
    sort(begin(x), end(x));
  }
  set<int> cands;
  for (int itr : {0, 1}) {
    for (auto [p, q] : a) {
      set<int> s;
      for (auto [x, y] : b) {
        if (p == x && q == y) continue;
        if (p == x or p == y) {
          s.insert(p);
        }
        if (q == x or q == y) {
          s.insert(q);
        }
      }
      if (int((s).size()) > 1) kek(-1);
      for (int x : s) cands.insert(x);
    }
    swap(a, b);
  }
  assert(!cands.empty());
  if (int((cands).size()) > 1) kek(0);
  for (int x : cands) kek(x);
}
