#include <bits/stdc++.h>
using namespace std;
template <typename T>
T Abs(T first) {
  return (first < 0 ? -first : first);
}
template <typename T>
T Sqr(T first) {
  return (first * first);
}
string plural(string s) {
  return (int((s).size()) && s[int((s).size()) - 1] == 'x' ? s + "en"
                                                           : s + "s");
}
const int INF = (int)1e9;
const long double EPS = 1e-12;
const long double PI = acos(-1.0);
bool Read(int &first) {
  char c, r = 0, n = 0;
  first = 0;
  for (;;) {
    c = getchar();
    if ((c < 0) && (!r)) return (0);
    if ((c == '-') && (!r))
      n = 1;
    else if ((c >= '0') && (c <= '9'))
      first = first * 10 + c - '0', r = 1;
    else if (r)
      break;
  }
  if (n) first = -first;
  return (1);
}
bool ReadLL(long long &first) {
  char c, r = 0, n = 0;
  first = 0;
  for (;;) {
    c = getchar();
    if ((c < 0) && (!r)) return (0);
    if ((c == '-') && (!r))
      n = 1;
    else if ((c >= '0') && (c <= '9'))
      first = first * 10 + c - '0', r = 1;
    else if (r)
      break;
  }
  if (n) first = -first;
  return (1);
}
int A1[20], A2[20], B1[20], B2[20];
int Com(int i, int j) {
  int a1 = A1[i], a2 = A2[i];
  int b1 = B1[j], b2 = B2[j];
  if (a1 == b1 && a2 == b2 || a1 == b2 && a2 == b1) return (0);
  if (a1 == b1 || a1 == b2) return (a1);
  if (a2 == b1 || a2 == b2) return (a2);
  return (0);
}
int main() {
  if (0) freopen("in.txt", "r", stdin);
  int N, M, K;
  int i, j, k, a, b, c, first, second, z;
  set<int> S;
  Read(N), Read(M);
  for (i = 0; i < N; i++) Read(A1[i]), Read(A2[i]);
  for (i = 0; i < M; i++) Read(B1[i]), Read(B2[i]);
  for (i = 0; i < N; i++) {
    int c = -1;
    for (j = 0; j < M; j++) {
      k = Com(i, j);
      if (k)
        if (c < 0)
          c = k;
        else if (c != k)
          c = 0;
    }
    if (c < 0) continue;
    if (!c) goto None;
    S.insert(c);
  }
  for (j = 0; j < M; j++) {
    int c = -1;
    for (i = 0; i < N; i++) {
      k = Com(i, j);
      if (k)
        if (c < 0)
          c = k;
        else if (c != k)
          c = 0;
    }
    if (c < 0) continue;
    if (!c) goto None;
    S.insert(c);
  }
  if (int((S).size()) == 1)
    printf("%d\n", *S.begin());
  else
    printf("0\n");
  return (0);
None:;
  printf("-1\n");
  return (0);
}
