#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> p1[16], p2[16];
vector<int> a1[16], a2[16];
void uniquify(vector<int>& v) {
  sort((v).begin(), (v).end());
  v.resize(unique((v).begin(), (v).end()) - v.begin());
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < (n); i++) cin >> p1[i].first >> p1[i].second;
  for (int i = 0; i < (m); i++) cin >> p2[i].first >> p2[i].second;
  for (int i = 0; i < (n); i++) p1[i] = minmax({p1[i].first, p1[i].second});
  for (int i = 0; i < (m); i++) p2[i] = minmax({p2[i].first, p2[i].second});
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      if (p1[i] == p2[j]) continue;
      int a, b, c, d;
      tie(a, b) = p1[i];
      tie(c, d) = p2[j];
      if (a == c or b == c) {
        a1[i].push_back(c);
      } else if (a == d or b == d) {
        a1[i].push_back(d);
      }
      if (a == c or a == d) {
        a2[j].push_back(a);
      } else if (b == c or b == d) {
        a2[j].push_back(b);
      }
    }
  }
  vector<int> ans1, ans2, ans;
  for (int i = 0; i < (n); i++)
    ans1.insert(ans1.end(), (a1[i]).begin(), (a1[i]).end());
  for (int i = 0; i < (m); i++)
    ans2.insert(ans2.end(), (a2[i]).begin(), (a2[i]).end());
  uniquify(ans1);
  uniquify(ans2);
  set_intersection((ans1).begin(), (ans1).end(), (ans2).begin(), (ans2).end(),
                   back_inserter(ans));
  if (ans.size() == 1) {
    cout << ans[0] << endl;
  } else {
    bool flag = true;
    for (int i = 0; i < (n); i++) {
      uniquify(a1[i]);
      flag &= a1[i].size() <= 1;
    }
    for (int i = 0; i < (m); i++) {
      uniquify(a2[i]);
      flag &= a2[i].size() <= 1;
    }
    if (flag) {
      cout << 0 << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
