#include <bits/stdc++.h>
using namespace std;
int a[20][20], b[20][20], x, y, ans, z, look[15][15], look2[15][15], l;
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    a[x][y] = a[y][x] = 1;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    b[x][y] = b[y][x] = 1;
  }
  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 (k == j || k == i) continue;
        if (a[i][j] == b[i][k] && a[i][j]) {
          look[i][j]++;
          look2[i][k]++;
          z = i;
        }
      }
    }
  }
  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", z);
  else {
    z = 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])) z = -1;
      }
    }
    printf("%d\n", z);
  }
}
