#include <bits/stdc++.h>
using namespace std;
int n, m, a[111][2], b[111][2];
int va[111], vb[111];
int main() {
  scanf("%d%d", &n, &m);
  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)
    if (a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);
  for (int i = 1; i <= m; ++i)
    if (b[i][0] > b[i][1]) swap(b[i][0], b[i][1]);
  memset(va, 0, sizeof(va));
  memset(vb, 0, sizeof(vb));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][0] == b[j][0] && a[i][1] == b[j][1]) continue;
      if (a[i][0] != b[j][0] && a[i][1] != b[j][1] && a[i][0] != b[j][1] &&
          a[i][1] != b[j][0])
        continue;
      if (a[i][0] == b[j][0] || a[i][0] == b[j][1]) {
        if (va[i] > 0 && a[i][0] != va[i] || va[i] < 0)
          va[i] = -2;
        else
          va[i] = a[i][0];
      } else {
        if (va[i] > 0 && a[i][1] != va[i] || va[i] < 0)
          va[i] = -2;
        else
          va[i] = a[i][1];
      }
    }
    if (!va[i]) va[i] = -1;
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (a[j][0] == b[i][0] && a[j][1] == b[i][1]) continue;
      if (a[j][0] != b[i][0] && a[j][1] != b[i][1] && a[j][0] != b[i][1] &&
          a[j][1] != b[i][0])
        continue;
      if (a[j][0] == b[i][0] || a[j][1] == b[i][0]) {
        if (vb[i] > 0 && b[i][0] != vb[i] || vb[i] < 0)
          vb[i] = -2;
        else
          vb[i] = b[i][0];
      } else {
        if (vb[i] > 0 && b[i][1] != vb[i] || vb[i] < 0)
          vb[i] = -2;
        else
          vb[i] = b[i][1];
      }
    }
    if (!vb[i]) vb[i] = -1;
  }
  bool a2 = 0, b2 = 0;
  for (int i = 1; i <= n; ++i) a2 |= va[i] == -2;
  for (int i = 1; i <= m; ++i) b2 |= vb[i] == -2;
  int cura = -1, curb = -1;
  bool diffa = 0, diffb = 0;
  for (int i = 1; i <= n; ++i) {
    if (va[i] > 0) {
      if (cura < 0)
        cura = va[i];
      else if (cura != va[i]) {
        diffa = 1;
        break;
      }
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (vb[i] > 0) {
      if (curb < 0)
        curb = vb[i];
      else if (curb != vb[i]) {
        diffb = 1;
        break;
      }
    }
  }
  if (!a2 && !b2 && !diffa && !diffb)
    printf("%d\n", cura);
  else if (!a2 && !b2)
    printf("0\n");
  else
    printf("-1\n");
  return 0;
}
