#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int look[202][202], look2[202][202];
int a[101][101], b[101][101];
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read();
    a[x][y]++;
    a[y][x]++;
  }
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    b[x][y]++;
    b[y][x]++;
  }
  int ans = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      if (i == j) continue;
      for (int k = 1; k <= 9; k++) {
        if (i == k || j == k) continue;
        if (a[i][j] && b[i][k]) {
          look[i][j]++;
          look2[i][k]++;
          ans = i;
        }
      }
    }
  }
  int l = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      if (look[i][j]) {
        l++;
        break;
      }
    }
  }
  if (l == 1) {
    printf("%d\n", ans);
    return 0;
  }
  l = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      if (i == j) continue;
      if ((look[i][j] && look[j][i]) || (look2[i][j] && look2[j][i])) l = -1;
    }
  }
  cout << l;
  return 0;
}
