#include <bits/stdc++.h>
using namespace std;
int N, M, na[20][2], nb[20][2], ca[11], cb[11];
pair<int, int> a[20], b[20];
int diff(int ai, int bi) {
  int res = 2;
  res -= (a[ai].first == b[bi].first);
  res -= (a[ai].first == b[bi].second);
  res -= (a[ai].second == b[bi].first);
  res -= (a[ai].second == b[bi].second);
  return res;
}
int main() {
  scanf("%d", &N);
  scanf("%d", &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i].first);
    scanf("%d", &a[i].second);
    ca[a[i].first]++;
    ca[a[i].second]++;
    if (a[i].second > a[i].first) swap(a[i].first, a[i].second);
  }
  int prev = -1, ans = 1;
  for (int i = 0; i < M; i++) {
    scanf("%d", &b[i].first);
    scanf("%d", &b[i].second);
    cb[b[i].first]++;
    cb[b[i].second]++;
    if (b[i].second > b[i].first) swap(b[i].first, b[i].second);
    for (int j = 0; j < N; j++) {
      if (diff(j, i) != 1) continue;
      int same = b[i].first;
      if (same != a[j].first && same != a[j].second) same = b[i].second;
      nb[i][same == b[i].second]++;
      na[j][same == a[j].second]++;
      if (prev == -1)
        prev = same;
      else if (prev != same)
        ans = 0;
    }
  }
  for (int i = 0; i < N; i++)
    if (na[i][0] && na[i][1]) ans = -1;
  for (int i = 0; i < M; i++)
    if (nb[i][0] && nb[i][1]) ans = -1;
  if (ans == 1) ans = prev;
  printf("%d\n", ans);
}
