#include <bits/stdc++.h>
using namespace std;
int read() {
  int w = 0, f = 1;
  char c = ' ';
  while (c < '0' || c > '9') c = getchar(), f = c == '-' ? -1 : f;
  while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar();
  return w * f;
}
struct node {
  int x, y;
} a[105], b[105];
int n, m;
int check(node x, node y) {
  if (x.x == y.x && x.y == y.y) return 0;
  if (x.x != y.x && x.x != y.y && x.y != y.x && x.y != y.y) return 0;
  if (x.x == y.x || x.x == y.y) return x.x;
  return x.y;
}
signed main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    a[i].x = read(), a[i].y = read();
    if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
  }
  for (int i = 1; i <= m; i++) {
    b[i].x = read(), b[i].y = read();
    if (b[i].x > b[i].y) swap(b[i].x, b[i].y);
  }
  int cnt = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    int nx = 0, fl = 0;
    for (int j = 1; j <= m; j++) {
      int x = check(a[i], b[j]);
      if (!x) continue;
      if (!nx)
        nx = x;
      else if (nx != x)
        fl = 1;
    }
    if (fl) return puts("-1"), 0;
    if (!nx) continue;
    if (!ans)
      cnt++, ans = nx;
    else if (nx != ans)
      cnt++;
  }
  if (!cnt) return puts("-1"), 0;
  int cnt2 = 0, as2 = 0;
  for (int i = 1; i <= m; i++) {
    int nx = 0, fl = 0;
    for (int j = 1; j <= n; j++) {
      int x = check(b[i], a[j]);
      if (!x) continue;
      if (!nx)
        nx = x;
      else if (nx != x)
        fl = 1;
    }
    if (fl) return puts("-1"), 0;
    if (!nx) continue;
    if (!as2)
      cnt2++, as2 = nx;
    else if (nx != as2)
      cnt2++;
  }
  if (!cnt2) return puts("-1"), 0;
  if (cnt == 1 && cnt2 == 1) return printf("%d\n", ans), 0;
  puts("0");
  return 0;
}
