#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int inf = 1e9;
const long long int N = 15;
pair<long long int, long long int> p[N], q[N];
long long int tmp;
long long int cm(long long int i, long long int j) {
  if (p[i].first == q[j].first && p[i].second == q[j].second)
    return 2;
  else if (p[i].first == q[j].first) {
    tmp = p[i].first;
    return 1;
  } else if (p[i].first == q[j].second) {
    tmp = p[i].first;
    return 1;
  } else if (p[i].second == q[j].first) {
    tmp = p[i].second;
    return 1;
  } else if (p[i].second == q[j].second) {
    tmp = p[i].second;
    return 1;
  }
  return 0;
}
signed main() {
  long long int n, m;
  scanf("%lld %lld", &n, &m);
  for (long long int i = 1; i <= n; i++) {
    scanf("%lld %lld", &p[i].first, &p[i].second);
    if (p[i].second < p[i].first) swap(p[i].first, p[i].second);
  }
  for (long long int i = 1; i <= m; i++) {
    scanf("%lld %lld", &q[i].first, &q[i].second);
    if (q[i].second < q[i].first) swap(q[i].first, q[i].second);
  }
  long long int cnt = 0;
  bool poss = 1;
  set<long long int> s;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= m; j++) {
      if (cm(i, j) != 1) continue;
      long long int c = tmp;
      long long int cnt1 = 0, cnt2 = 0, x = -1, y = -1;
      bool yes = 1;
      for (long long int k = 1; k <= n; k++) {
        if (cm(k, j) == 1) {
          if (x == -1)
            x = tmp;
          else if (x != tmp)
            yes = 0;
        }
      }
      for (long long int k = 1; k <= m; k++) {
        if (cm(i, k) == 1) {
          if (y == -1)
            y = tmp;
          else if (y != tmp)
            yes = 0;
        }
      }
      if (yes && x == y) {
        s.insert(x);
      }
      if ((yes && x != y) || !yes) {
        poss = 0;
        break;
      }
    }
    if (!poss) break;
  }
  if (!poss) return puts("-1"), 0;
  if (poss && s.size() == 1)
    printf("%lld", *s.begin());
  else
    puts("0");
  return 0;
}
