#include <bits/stdc++.h>
using namespace std;
int xa[22], xb[22], ya[22], yb[22];
int n, m, ans, suma, sumb;
set<int> s1, s2;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d %d", &xa[i], &xb[i]);
  for (int i = 1; i <= m; i++) scanf("%d %d", &ya[i], &yb[i]);
  for (int i = 1; i <= n; i++) {
    int res = 0, tped = 0;
    for (int j = 1; j <= m; j++) {
      int gp = 0, p;
      if (xa[i] == ya[j]) gp++, p = xa[i];
      if (xa[i] == yb[j]) gp++, p = xa[i];
      if (xb[i] == ya[j]) gp++, p = xb[i];
      if (xb[i] == yb[j]) gp++, p = xb[i];
      if (gp == 1) {
        if (tped != p) res++;
        tped = p;
      }
    }
    if (res > 1) {
      printf("-1");
      return 0;
    }
    if (res == 1) s1.insert(tped);
  }
  ans = 0;
  for (int i = 1; i <= m; i++) {
    int res = 0, tped = 0;
    for (int j = 1; j <= n; j++) {
      int gp = 0, p;
      if (ya[i] == xa[j]) gp++, p = ya[i];
      if (ya[i] == xb[j]) gp++, p = ya[i];
      if (yb[i] == xa[j]) gp++, p = yb[i];
      if (yb[i] == xb[j]) gp++, p = yb[i];
      if (gp == 1) {
        if (p != tped) res++;
        tped = p;
      }
    }
    if (res > 1) {
      printf("-1");
      return 0;
    }
    if (res == 1) {
      s2.insert(tped);
    }
  }
  if (s1.size() == 1) {
    printf("%d", *s1.begin());
    return 0;
  }
  if (s2.size() == 0) {
    printf("%d", *s2.begin());
    return 0;
  }
  printf("0");
  return 0;
}
