#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int a[N], b[N];
bool pa[N], pb[N];
int main() {
  int n, m, ans1 = 0, ans2 = 0;
  bool qwq = 0, qaq = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 2 * n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < 2 * m; i++) scanf("%d", &b[i]);
  for (int i = 0; i < 2 * n; i++) {
    int cnt = 0;
    for (int j = 0; j < 2 * m; j++)
      if (a[i] == b[j] && a[i ^ 1] != b[j ^ 1]) pa[i] = 1;
    if (pa[i] & pa[i ^ 1]) qwq = 1;
    if (pa[i] && ans1 && ans1 != a[i])
      qaq = 1;
    else if (pa[i])
      ans1 = a[i];
  }
  for (int i = 0; i < 2 * m; i++) {
    int cnt = 0;
    for (int j = 0; j < 2 * n; j++)
      if (b[i] == a[j] && b[i ^ 1] != a[j ^ 1]) pb[i] = 1;
    if (pb[i] & pb[i ^ 1]) qwq = 1;
    if (pb[i] && ans2 && ans2 != b[i])
      qaq = 1;
    else if (pb[i])
      ans2 = b[i];
  }
  if (qwq == 1)
    printf("-1");
  else if (qaq == 1)
    printf("0");
  else
    printf("%d", ans1);
  return 0;
}
