#include <bits/stdc++.h>
using namespace std;
int p1[10][13], p2[10][13];
bool fl = false;
int c1[10], c2[10];
int len, n, m;
int sum = 0, ans;
int main() {
  memset(c1, 0, sizeof(c1));
  memset(c2, 0, sizeof(c2));
  memset(p1, -1, sizeof(p1));
  memset(p2, -1, sizeof(p2));
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    p1[a][c1[a]++] = b;
    p1[b][c1[b]++] = a;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    p2[a][c2[a]++] = b;
    p2[b][c2[b]++] = a;
  }
  for (int i = 1; i <= 9; i++) {
    if (p1[i][0] == -1 || p2[i][0] == -1) {
      continue;
    }
    for (int j = 0; j < c1[i]; j++) {
      for (int k = 0; k < c2[i]; k++) {
        if (p1[i][j] == p2[i][k]) {
          continue;
        }
        for (int t = 0; t < c2[p1[i][j]]; t++) {
          if (p2[p1[i][j]][t] != -1 && p2[p1[i][j]][t] != i) {
            fl = true;
            break;
          }
        }
        for (int t = 0; t < c1[p2[i][k]]; t++) {
          if (p1[p2[i][k]][t] != -1 && p1[p2[i][k]][t] != i) {
            fl = true;
            break;
          }
        }
        if (fl) break;
        if (i != ans) sum++;
        ans = i;
      }
      if (fl) break;
    }
  }
  if (fl) {
    printf("-1\n");
  } else {
    if (sum == 1) printf("%d\n", ans);
    if (sum > 1) printf("0\n");
  }
  return 0;
}
