#include <bits/stdc++.h>
using namespace std;
int n, m, ans, a[30][2], b[30][2];
int check(int x, int y) {
  if (a[x][0] == b[y][0] && a[x][1] == b[y][1]) return -1;
  if (a[x][0] == b[y][1] && a[x][1] == b[y][0]) return -1;
  if (a[x][0] == b[y][0] || a[x][0] == b[y][1]) return a[x][0];
  if (a[x][1] == b[y][0] || a[x][1] == b[y][1]) return a[x][1];
  return -1;
}
int main() {
  scanf("%d%d", &n, &m);
  int flg = 1;
  for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i][0], &a[i][1]);
  for (int i = 1; i <= m; ++i) scanf("%d%d", &b[i][0], &b[i][1]);
  for (int i = 1; i <= n; ++i) {
    int num = 0;
    for (int j = 1; j <= m; ++j) {
      int x = check(i, j);
      if (x == -1) continue;
      if (!num)
        num = x;
      else if (num != x) {
        puts("-1");
        return 0;
      }
    }
    if (!num) continue;
    if (!ans)
      ans = num;
    else if (ans != num)
      flg = 0;
  }
  for (int i = 1; i <= m; ++i) {
    int num = 0;
    for (int j = 1; j <= n; ++j) {
      int x = check(j, i);
      if (x == -1) continue;
      if (!num)
        num = x;
      else if (num != x) {
        puts("-1");
        return 0;
      }
    }
    if (!num) continue;
    if (!ans)
      ans = num;
    else if (ans != num)
      flg = 0;
  }
  printf("%d\n", flg ? ans : 0);
  return 0;
}
