#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 + 5;
int n, m, a[maxn], b[maxn], c[maxn], d[maxn];
set<int> st;
int judge(int x, int y) {
  if (a[x] == c[y] && b[x] != d[y]) return a[x];
  if (a[x] == d[y] && b[x] != c[y]) return a[x];
  if (b[x] == c[y] && a[x] != d[y]) return b[x];
  if (b[x] == d[y] && a[x] != c[y]) return b[x];
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
  for (int i = 1; i <= m; ++i) cin >> c[i] >> d[i];
  int ok = 1;
  for (int i = 1; i <= n; ++i) {
    int cnt = 0;
    set<int> tmp;
    for (int j = 1; j <= m; ++j) {
      int x = judge(i, j);
      if (x != -1) {
        cnt++;
        tmp.insert(x);
        st.insert(x);
      }
    }
    if (tmp.size() > 1) ok = 0;
  }
  for (int i = 1; i <= m; ++i) {
    int cnt = 0;
    set<int> tmp;
    for (int j = 1; j <= n; ++j) {
      int x = judge(j, i);
      if (x != -1) {
        cnt++;
        tmp.insert(x);
        st.insert(x);
      }
    }
    if (tmp.size() > 1) ok = 0;
  }
  if (st.size() == 1)
    cout << *st.begin() << endl;
  else {
    cout << (ok == 1 ? 0 : -1) << endl;
  }
  return 0;
}
