#include <bits/stdc++.h>
using namespace std;
int ans_num;
int func(vector<int> va, vector<int> vb) {
  int f = 0;
  set<int> all_know;
  for (int i = 0, si = va.size(); i < si; i += 2) {
    int tf = 0;
    set<int> know_s;
    for (int j = 0, sj = vb.size(); j < sj; j += 2) {
      if ((va[i] == vb[j] && va[i + 1] != vb[j + 1]) ||
          (va[i] == vb[j + 1] && va[i + 1] != vb[j])) {
        know_s.insert(va[i]);
      }
      if ((va[i + 1] == vb[j] && va[i] != vb[j + 1]) ||
          (va[i + 1] == vb[j + 1] && va[i] != vb[j])) {
        know_s.insert(va[i + 1]);
      }
    }
    for (auto it : know_s) {
      all_know.insert(it);
    }
    f |= (know_s.size() > 1 ? 3 : 0);
  }
  f |= (all_know.size() > 1 ? 1 : 0);
  ans_num = *all_know.begin();
  return f;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  vector<int> va, vb;
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    va.push_back(a);
    va.push_back(b);
  }
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    vb.push_back(a);
    vb.push_back(b);
  }
  int ans = func(va, vb) | func(vb, va);
  cout << (ans == 3 ? -1 : (ans == 0 ? ans_num : 0)) << endl;
  return 0;
}
