#include <bits/stdc++.h>
using namespace std;
const int N = 51;
int n, m;
bool visa[N][N], visb[N][N];
int match1[N][N], match2[N][N];
int ans, flag;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
int main() {
  n = read(), m = read();
  int x, y;
  for (int i = 1; i <= n; ++i) {
    x = read(), y = read();
    visa[x][y] = 1;
    visa[y][x] = 1;
  }
  for (int i = 1; i <= m; ++i) {
    x = read(), y = read();
    visb[x][y] = 1;
    visb[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 == i || k == j) continue;
        if (visa[i][j] == visb[i][k] && visa[i][j]) {
          ++match1[i][j];
          ++match2[i][k];
          x = i;
        }
      }
    }
  }
  for (int i = 1; i <= 9; ++i) {
    for (int j = 1; j <= 9; ++j) {
      if (match1[i][j]) {
        ++flag;
        break;
      }
    }
  }
  if (flag == 1) {
    cout << x << endl;
    return 0;
  }
  for (int i = 1; i <= 9; ++i) {
    for (int j = 1; j <= 9; ++j) {
      if (i == j) continue;
      if ((match1[i][j] && match1[j][i]) || (match2[i][j] && match2[j][i])) {
        puts("-1");
        return 0;
      }
    }
  }
  puts("0");
  return 0;
}
