#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return f ? x : -x;
}
long long n, m, ans, s = 1, num;
bool exia[20][20], exib[20][20], s1[20][20], s2[20][20];
signed main() {
  n = read(), m = read();
  for (long long i = 1; i <= n; i++) {
    long long x = read(), y = read();
    exia[x][y] = exia[y][x] = 1;
  }
  for (long long i = 1; i <= m; i++) {
    long long x = read(), y = read();
    exib[x][y] = exib[y][x] = 1;
  }
  for (long long i = 1; i <= 9; i++) {
    for (long long j = 1; j <= 9; j++) {
      if (i == j) continue;
      for (long long k = 1; k <= 9; k++) {
        if (i == k || j == k) continue;
        if (exia[i][j] && exib[i][k]) {
          s1[i][j] = s2[i][k] = 1;
          ans = i;
          if (num && num != i) s = 0;
          num = i;
        }
      }
    }
  }
  if (s)
    printf("%lld", ans);
  else {
    ans = 0;
    for (long long i = 1; i <= 9; i++) {
      for (long long j = 1; j <= 9; j++) {
        if (i == j) continue;
        if (s1[i][j] && s1[j][i]) {
          ans = -1;
          break;
        }
        if (s2[i][j] && s2[j][i]) {
          ans = -1;
          break;
        }
      }
    }
    printf("%lld", ans);
  }
  return 0;
}
