#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, n, t = 1, k = 0, cnt = 0, mini = LLONG_MAX, maxi = LLONG_MIN,
                     ans = 0;
  long long m;
  cin >> n >> m;
  vector<pair<long long, long long> > a, b;
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    a.push_back(make_pair(j, k));
  }
  for (i = 0; i < m; i++) {
    cin >> j >> k;
    b.push_back(make_pair(j, k));
  }
  map<pair<long long, long long>, pair<long long, long long> > mm, mm1;
  for (i = 0; i < a.size(); i++) {
    pair<long long, long long> u = a[i];
    for (j = 0; j < b.size(); j++) {
      if (a[i].first == b[j].first && a[i].second != b[j].second ||
          a[i].first == b[j].second && a[i].second != b[j].first) {
        if (mm.find(a[i]) == mm.end()) {
          pair<long long, long long> pr;
          pr.first = 1, pr.second = 0;
          mm[a[i]] = pr;
        } else {
          auto pr = mm[a[i]];
          pr.first += 1;
          mm[a[i]] = pr;
        }
      } else if (a[i].second == b[j].second && a[i].first != b[j].first ||
                 a[i].second == b[j].first && a[i].first != b[j].second) {
        if (mm.find(a[i]) == mm.end()) {
          pair<long long, long long> pr;
          pr.first = 0, pr.second = 1;
          mm[a[i]] = pr;
        } else {
          auto pr = mm[a[i]];
          pr.second += 1;
          mm[a[i]] = pr;
        }
      }
    }
  }
  long long cnta = 0, cntb = 0;
  set<long long> ca, cb;
  for (auto it : mm) {
    if (it.second.first >= 1 && it.second.second >= 1) return cout << -1, 0;
    if (it.second.first >= 1 || it.second.second >= 1) {
      if (it.second.first >= 1)
        ca.insert(it.first.first);
      else
        ca.insert(it.first.second);
    }
  }
  for (i = 0; i < b.size(); i++) {
    pair<long long, long long> u = b[i];
    for (j = 0; j < a.size(); j++) {
      if (b[i].first == a[j].first && b[i].second != a[j].second ||
          b[i].first == a[j].second && b[i].second != a[j].first) {
        if (mm1.find(b[i]) == mm1.end()) {
          pair<long long, long long> pr;
          pr.first = 1, pr.second = 0;
          mm1[b[i]] = pr;
        } else {
          auto pr = mm1[b[i]];
          pr.first += 1;
          mm1[b[i]] = pr;
        }
      } else if (b[i].second == a[j].second && b[i].first != a[j].first ||
                 b[i].second == a[j].first && b[i].first != a[j].second) {
        if (mm1.find(b[i]) == mm1.end()) {
          pair<long long, long long> pr;
          pr.first = 0, pr.second = 1;
          mm1[b[i]] = pr;
        } else {
          auto pr = mm1[b[i]];
          pr.second += 1;
          mm1[b[i]] = pr;
        }
      }
    }
  }
  for (auto it : mm1) {
    if (it.second.first >= 1 && it.second.second >= 1) return cout << -1, 0;
    if (it.second.first >= 1 || it.second.second >= 1) {
      if (it.second.first >= 1)
        cb.insert(it.first.first);
      else
        cb.insert(it.first.second);
    }
  }
  if (ca.size() == 1 && cb.size() == 1) return cout << *ca.begin(), 0;
  return cout << 0, 0;
  return 0;
}
