#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <typename T>
T gcd(T m, T n) {
  while (n) {
    T t = m % n;
    m = n;
    n = t;
  };
  return m;
}
template <typename T>
T exgcd(T a, T b, T& sa, T& ta) {
  T q, r, sb = 0, tb = 1, sc, tc;
  sa = 1, ta = 0;
  if (b) do
      q = a / b, r = a - q * b, a = b, b = r, sc = sa - q * sb, sa = sb,
      sb = sc, tc = ta - q * tb, ta = tb, tb = tc;
    while (b);
  return a;
}
template <typename T>
T mul_inv(T a, T b) {
  T t1 = a, t2 = b, t3;
  T v1 = 1, v2 = 0, v3;
  T x;
  while (t2 != 1)
    x = t1 / t2, t3 = t1 - x * t2, v3 = v1 - x * v2, t1 = t2, t2 = t3, v1 = v2,
    v2 = v3;
  return (v2 + b) % b;
}
template <typename T>
T powmod(T a, T b, T MOD) {
  if (b < 0) return 0;
  T rv = 1;
  while (b) (b % 2) && (rv = (rv * a) % MOD), a = a * a % MOD, b /= 2;
  return rv;
}
int n, m;
int r = 0;
int nbr[2][20];
inline int bc(int x) {
  int r = 0;
  while (x) x -= x & -x, r++;
  return r;
}
void solve() {
  r = 0;
  bool multi = false;
  for (int i = 0; i < n; i++) {
    int a = nbr[0][i];
    int am = 0;
    for (int j = 0; j < m; j++) {
      int b = nbr[1][j];
      if (bc(a | b) == 3 && (a & b)) {
        am |= a & b;
      }
    }
    if (am == 0) continue;
    if (am == (am & -am))
      r |= am;
    else {
      r = 0;
      multi = true;
      break;
    }
  }
  for (int j = 0; j < m; j++) {
    int b = nbr[1][j];
    int bm = 0;
    for (int i = 0; i < n; i++) {
      int a = nbr[0][i];
      if (bc(a | b) == 3 && (a & b)) {
        bm |= a & b;
      }
    }
    if (bm == 0) continue;
    if (bm == (bm & -bm))
      r |= bm;
    else {
      r = 0;
      multi = true;
      break;
    }
  }
  if (multi) r = 0;
}
int main(int argc, char* argv[]) {
  std::cin.sync_with_stdio(false);
  std::cin.tie(nullptr);
  (cin >> n >> m);
  {
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      nbr[0][i] = (1 << x) | (1 << y);
    }
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      nbr[1][i] = (1 << x) | (1 << y);
    }
    r = 0;
    solve();
    if (r == 0)
      cout << -1 << endl;
    else if (r != (r & -r))
      cout << 0 << endl;
    else {
      int ri = 1;
      for (; ri <= 9; ri++) {
        if (r & (1 << ri)) break;
      }
      cout << ri << endl;
    }
  }
  return 0;
}
