#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
int main() {
  vector<PII> A(20), B(20);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (n); ++i) {
    scanf("%d%d", &A[i].first, &A[i].second);
    if (A[i].first > A[i].second) swap(A[i].first, A[i].second);
  }
  for (int i = (1); i <= (m); ++i) {
    scanf("%d%d", &B[i].first, &B[i].second);
    if (B[i].first > B[i].second) swap(B[i].first, B[i].second);
  }
  auto same = [&](const PII a, const PII b) {
    vector<int> R;
    if (a.first == b.first || a.first == b.second) R.push_back(a.first);
    if (a.second == b.first || a.second == b.second) R.push_back(a.second);
    return R;
  };
  set<int> C;
  bool flag1 = true, flag2 = true;
  for (int i = (1); i <= (n); ++i) {
    set<int> T;
    for (int j = (1); j <= (m); ++j) {
      auto S = same(A[i], B[j]);
      if (S.empty() || S.size() == 2) continue;
      int t = S[0];
      C.insert(t);
      T.insert(t);
    }
    if (T.size() > 1) flag1 = false;
  }
  if (C.size() == 1) {
    cout << *C.begin() << endl;
    return 0;
  }
  for (int i = (1); i <= (m); ++i) {
    set<int> T;
    for (int j = (1); j <= (n); ++j) {
      auto S = same(A[j], B[i]);
      if (S.empty() || S.size() == 2) continue;
      int t = S[0];
      T.insert(t);
    }
    if (T.size() > 1) flag2 = false;
  }
  if (flag1 && flag2) return !printf("0\n");
  printf("-1\n");
  return 0;
}
