#include <bits/stdc++.h>
inline int readInt() {
  int ans = 0, c, f = 1;
  while (!isdigit(c = getchar()))
    if (c == '-') f *= -1;
  do ans = ans * 10 + c - '0';
  while (isdigit(c = getchar()));
  return ans * f;
}
const int N = 20;
int n, A1[N], B1[N];
int m, A2[N], B2[N];
bool C1[N][10], C2[N][10], ans[10];
int match(int i, int j) {
  int a = A1[i], b = B1[i], c = A2[j], d = B2[j];
  if (a == c && b != d) return a;
  if (a == d && b != c) return a;
  if (b == d && a != c) return b;
  if (b == c && a != d) return b;
  return 0;
}
bool chk(bool A[10]) {
  int t = 0;
  for (int i = 1; i <= 9; ++i) t += A[i];
  return t <= 1;
}
int main() {
  n = readInt();
  m = readInt();
  for (int i = 0; i < n; ++i) A1[i] = readInt(), B1[i] = readInt();
  for (int i = 0; i < m; ++i) A2[i] = readInt(), B2[i] = readInt();
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (int t = match(i, j)) ans[t] = C1[i][t] = C2[j][t] = true;
  if (chk(ans))
    for (int i = 1; i <= 9; ++i)
      if (ans[i]) return printf("%d\n", i) & 0;
  for (int i = 0; i < n; ++i)
    if (!chk(C1[i])) return puts("-1") & 0;
  for (int i = 0; i < m; ++i)
    if (!chk(C2[i])) return puts("-1") & 0;
  return puts("0") & 0;
}
