#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long double eps = 1e-10;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> P(n), Q(m);
  for (int i = (int)(0); i < (int)(n); i++) cin >> P[i].first >> P[i].second;
  for (int i = (int)(0); i < (int)(m); i++) cin >> Q[i].first >> Q[i].second;
  vector<vector<pair<int, int>>> G(n + m);
  set<int> cand;
  for (int i = (int)(0); i < (int)(n); i++)
    for (int j = (int)(0); j < (int)(m); j++) {
      set<int> st;
      st.insert(P[i].first);
      st.insert(P[i].second);
      int cnt = st.count(Q[j].first) + st.count(Q[j].second);
      if (cnt == 0 || cnt == 2) continue;
      int match = (st.count(Q[j].first) ? Q[j].first : Q[j].second);
      G[i].push_back({n + j, match});
      G[n + j].push_back({i, match});
      cand.insert(match);
    }
  if (cand.size() == 0) {
    assert(false);
  } else if (cand.size() == 1) {
    cout << *cand.begin() << '\n';
  } else {
    bool ok1 = true, ok2 = true;
    for (int i = (int)(0); i < (int)(n); i++) {
      if (G[i].empty()) continue;
      set<int> cand2;
      for (auto [j, v] : G[i]) cand2.insert(v);
      if (cand2.size() > 1) ok1 = false;
    }
    for (int i = (int)(0); i < (int)(m); i++) {
      if (G[n + i].empty()) continue;
      set<int> cand2;
      for (auto [j, v] : G[n + i]) cand2.insert(v);
      if (cand2.size() > 1) ok2 = false;
    }
    if (ok1 && ok2)
      cout << 0 << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}
