#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
pair<int, int> par[2][maxn];
set<int> st, stt;
int pa(const pair<int, int> &a, const pair<int, int> &b, int &num) {
  int res = 0;
  if (a.first == b.first) {
    res++;
    num = a.first;
  }
  if (a.second == b.second) {
    res++;
    num = a.second;
  }
  if (a.first == b.second) {
    res++;
    num = a.first;
  }
  if (a.second == b.first) {
    res++;
    num = a.second;
  }
  return res;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    bool flag = false;
    st.clear();
    for (int i = 0; i < n; i++) cin >> par[0][i].first >> par[0][i].second;
    for (int i = 0; i < m; i++) cin >> par[1][i].first >> par[1][i].second;
    for (int i = 0; i < n; i++) {
      stt.clear();
      for (int j = 0; j < m; j++) {
        int num;
        int cnt = pa(par[0][i], par[1][j], num);
        if (cnt == 1) {
          st.insert(num);
          stt.insert(num);
        }
      }
      if ((int)stt.size() > 1) {
        flag = true;
      }
    }
    for (int i = 0; i < m; i++) {
      stt.clear();
      for (int j = 0; j < n; j++) {
        int num;
        int cnt = pa(par[1][i], par[0][j], num);
        if (cnt == 1) {
          st.insert(num);
          stt.insert(num);
        }
      }
      if ((int)stt.size() > 1) {
        flag = true;
      }
    }
    if (flag) {
      cout << "-1" << endl;
      continue;
    }
    if ((int)st.size() != 1) {
      cout << 0 << endl;
      continue;
    }
    cout << *st.begin() << endl;
  }
  return 0;
}
