#include <bits/stdc++.h>
using namespace std;
template <class U, class T>
void Max(U &first, T second) {
  if (first < second) first = second;
}
template <class U, class T>
void Min(U &first, T second) {
  if (first > second) first = second;
}
template <class T>
void add(int &a, T b) {
  a = (a + b) % 1000000007;
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 1000000007;
    a = 1LL * a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
int pow(int a, int b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
pair<int, int> a[12], b[12];
int vis[10], now;
int main() {
  srand(time(NULL));
  int T, ca = 0, m = 0, k, i, j, K, n;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 0; i < m; i++) scanf("%d%d", &b[i].first, &b[i].second);
  set<int> s, sa, sb;
  int p = 0, q = 0;
  for (int i = 0; i < n; i++) {
    now++;
    vis[a[i].first] = vis[a[i].second] = now;
    s.clear();
    for (int j = 0; j < m; j++) {
      if (vis[b[j].first] == now && vis[b[j].second] == now) continue;
      if (vis[b[j].first] == now) s.insert(b[j].first);
      if (vis[b[j].second] == now) s.insert(b[j].second);
    }
    if (s.size() == 1)
      p |= 1, sa.insert(*s.begin());
    else if (s.size() > 1)
      p |= 2;
  }
  swap(a, b);
  swap(n, m);
  for (int i = 0; i < n; i++) {
    now++;
    vis[a[i].first] = vis[a[i].second] = now;
    s.clear();
    for (int j = 0; j < m; j++) {
      if (vis[b[j].first] == now && vis[b[j].second] == now) continue;
      if (vis[b[j].first] == now) s.insert(b[j].first);
      if (vis[b[j].second] == now) s.insert(b[j].second);
    }
    if (s.size() == 1)
      q |= 1, sb.insert(*s.begin());
    else if (s.size() > 1)
      q |= 2;
  }
  if (p == 1 && q == 1) {
    if (sa.size() == 1) {
      printf("%d\n", *sa.begin());
    } else if (sb.size() == 1) {
      printf("%d\n", *sb.begin());
    } else
      puts("0");
  } else
    puts("-1");
}
