#include <bits/stdc++.h>
using namespace std;
int N, M, i, j;
pair<int, int> A[13], B[13];
set<int> ANS, SOL;
set<int>::iterator it;
int main() {
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> A[i].first >> A[i].second;
    if (A[i].first > A[i].second) swap(A[i].first, A[i].second);
  }
  for (i = 1; i <= M; i++) {
    cin >> B[i].first >> B[i].second;
    if (B[i].first > B[i].second) swap(B[i].first, B[i].second);
  }
  for (i = 1; i <= N; i++) {
    SOL.clear();
    for (j = 1; j <= M; j++) {
      if (A[i].first == B[j].first && A[i].second == B[j].second) continue;
      if (A[i].first != B[j].first && A[i].first != B[j].second &&
          A[i].second != B[j].first && A[i].second != B[j].second)
        continue;
      if (A[i].first == B[j].first) SOL.insert(A[i].first);
      if (A[i].first == B[j].second) SOL.insert(A[i].first);
      if (A[i].second == B[j].first) SOL.insert(A[i].second);
      if (A[i].second == B[j].second) SOL.insert(A[i].second);
    }
    if (SOL.size() == 1) {
      it = SOL.begin();
      ANS.insert(*it);
    }
    if (SOL.size() > 1) {
      cout << -1;
      return 0;
    }
  }
  for (j = 1; j <= M; j++) {
    SOL.clear();
    for (i = 1; i <= N; i++) {
      if (A[i].first == B[j].first && A[i].second == B[j].second) continue;
      if (A[i].first != B[j].first && A[i].first != B[j].second &&
          A[i].second != B[j].first && A[i].second != B[j].second)
        continue;
      if (A[i].first == B[j].first) SOL.insert(A[i].first);
      if (A[i].first == B[j].second) SOL.insert(A[i].first);
      if (A[i].second == B[j].first) SOL.insert(A[i].second);
      if (A[i].second == B[j].second) SOL.insert(A[i].second);
    }
    if (SOL.size() == 1) {
      it = SOL.begin();
      ANS.insert(*it);
    }
    if (SOL.size() > 1) {
      cout << -1;
      return 0;
    }
  }
  if (ANS.size() == 0) cout << -1;
  if (ANS.size() == 1) {
    it = ANS.begin();
    cout << *it;
  }
  if (ANS.size() > 1) cout << 0;
  return 0;
}
