#include <bits/stdc++.h>
using namespace std;
int N, M;
pair<int, int> a[15], b[15];
set<int> seen;
bool checkIt(pair<int, int> q, pair<int, int> r, pair<int, int> s) {
  if (q.first == r.first && q.second == r.second) return false;
  if (q.first == s.first && q.second == s.second) return false;
  if ((q.first == r.first || q.first == r.second) &&
      (q.second == s.first || q.second == s.second))
    return true;
  if ((q.first == s.first || q.first == s.second) &&
      (q.second == r.first || q.second == r.second))
    return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(18);
  cin >> N >> M;
  for (int i = 0; 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 (int i = 0; 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 (int i = 0; i < N; i++) {
    bool firsty = false, secondy = false;
    for (int j = 0; 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) firsty = true;
      if (a[i].second == b[j].first || a[i].second == b[j].second)
        secondy = true;
    }
    if (firsty) seen.insert(a[i].first);
    if (secondy) seen.insert(a[i].second);
  }
  if (seen.size() == 1) {
    cout << *seen.begin() << '\n';
    return 0;
  } else
    seen.clear();
  for (int i = 0; i < M; i++) {
    bool firsty = false, secondy = false;
    for (int j = 0; j < N; j++) {
      if (b[i].first == a[j].first && b[i].second == a[j].second) continue;
      if (b[i].first == a[j].first || b[i].first == a[j].second) firsty = true;
      if (b[i].second == a[j].first || b[i].second == a[j].second)
        secondy = true;
    }
    if (firsty) seen.insert(b[i].first);
    if (secondy) seen.insert(b[i].second);
  }
  if (seen.size() == 1) {
    cout << *seen.begin() << '\n';
    return 0;
  } else
    seen.clear();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      for (int k = 0; k < M; k++)
        if (j != k && checkIt(a[i], b[j], b[k])) {
          cout << "-1\n";
          return 0;
        }
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < N; k++)
        if (j != k && checkIt(b[i], a[j], a[k])) {
          cout << "-1\n";
          return 0;
        }
  cout << "0\n";
  return 0;
}
