#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool smin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool smax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const long long N = 20;
long long n, m, x[N], y[N], a[N], b[N];
bool mark[10];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; ++i) cin >> x[i] >> y[i];
  for (long long i = 0; i < m; ++i) cin >> a[i] >> b[i];
  for (long long i = 0; i < n; ++i) {
    if (x[i] > y[i]) swap(x[i], y[i]);
  }
  for (long long i = 0; i < m; ++i) {
    if (a[i] > b[i]) swap(a[i], b[i]);
  }
  for (long long i = 0; i < n; ++i) {
    set<long long> st;
    for (long long j = 0; j < m; ++j) {
      if (x[i] == a[j] && y[i] == b[j]) continue;
      if (x[i] == a[j] || x[i] == b[j]) st.insert(x[i]);
      if (y[i] == a[j] || y[i] == b[j]) st.insert(y[i]);
    }
    if ((long long)st.size() > 1) return cout << -1 << '\n', 0;
    if (!st.empty())
      for (long long x : st) mark[x] = 1;
  }
  for (long long i = 0; i < m; ++i) {
    set<long long> st;
    for (long long j = 0; j < n; ++j) {
      if (a[i] == x[j] && b[i] == y[j]) continue;
      if (a[i] == x[j] || a[i] == y[j]) st.insert(a[i]);
      if (b[i] == x[j] || b[i] == y[j]) st.insert(b[i]);
    }
    if ((long long)st.size() > 1) return cout << -1 << '\n', 0;
    if (!st.empty())
      for (long long x : st) mark[x] = 1;
  }
  long long cnt = 0, ch = -1;
  for (long long i = 0; i < 10; ++i) {
    if (mark[i]) {
      ++cnt;
      ch = i;
    }
  }
  if (cnt >= 2) ch = 0;
  cout << ch << '\n';
}
