#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b, i;
} stu[30];
int p[30];
vector<int> g[30];
bool flag;
void solve(node x, int pl) {
  if (p[x.i] != 0 && p[x.i] != pl) {
    flag = false;
  }
  p[x.i] = pl;
}
int can(node x, node y) {
  if (x.a == y.a && x.b != y.b) {
    solve(x, 1);
    solve(y, 1);
    return x.a;
  }
  if (x.a == y.b && x.b != y.a) {
    solve(x, 1);
    solve(y, 2);
    return x.a;
  }
  if (x.b == y.a && x.a != y.b) {
    solve(x, 2);
    solve(y, 1);
    return x.b;
  }
  if (x.b == y.b && x.a != y.a) {
    solve(x, 2);
    solve(y, 2);
    return x.b;
  }
  return 0;
}
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    flag = true;
    memset(p, 0, sizeof(p));
    for (int i = 0; i < 30; i++) g[i].clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &stu[i].a, &stu[i].b);
      stu[i].i = i;
    }
    for (int i = n + 1; i <= n + m; i++) {
      scanf("%d%d", &stu[i].a, &stu[i].b);
      stu[i].i = i;
      for (int j = 1; j <= n; j++) {
        if (can(stu[i], stu[j]) > 0) {
          g[i].push_back(j);
          g[j].push_back(i);
        }
      }
    }
    int num = 0;
    for (int i = 1; i <= n + m; i++) {
      if (g[i].size() >= 1) {
        if (num == 0)
          num = can(stu[i], stu[g[i][0]]);
        else if (num != can(stu[i], stu[g[i][0]]))
          num = -1;
      }
    }
    if (flag && num != -1 && num != 0) {
      printf("%d\n", num);
    } else if (flag) {
      printf("0\n");
    } else
      printf("-1\n");
  }
}
