#include <bits/stdc++.h>
using namespace std;
void smain();
int main() {
  ios_base::sync_with_stdio(0);
  smain();
  return 0;
}
inline set<pair<long long, long long>> prep(
    vector<pair<long long, long long>> &a) {
  set<pair<long long, long long>> res;
  for (auto i : a) res.emplace(min(i.first, i.second), max(i.first, i.second));
  return res;
}
inline bool check(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first == b.first || a.first == b.second || a.second == b.first ||
         a.second == b.second;
}
long long solve(vector<pair<long long, long long>> &va,
                vector<pair<long long, long long>> &vb) {
  auto a = prep(va);
  auto b = prep(vb);
  vector<long long> cand;
  for (auto it : a) {
    for (auto jt : b)
      if (it != jt && check(it, jt)) {
        long long ca = 0;
        long long cb = 0;
        for (auto t : a)
          if (t != jt && check(t, jt)) {
            if (jt.first == t.first || jt.first == t.second) cb |= 1;
            if (jt.second == t.first || jt.second == t.second) cb |= 2;
          }
        for (auto t : b)
          if (t != it && check(t, it)) {
            if (it.first == t.first || it.first == t.second) ca |= 1;
            if (it.second == t.first || it.second == t.second) ca |= 2;
          }
        if (ca == 3 || cb == 3) return -1;
        if (it.first == jt.first || it.first == jt.second)
          cand.push_back(it.first);
        else
          cand.push_back(it.second);
      }
  }
  sort(cand.begin(), cand.end());
  cand.resize(unique(cand.begin(), cand.end()) - cand.begin());
  if (cand.size() == 1) return cand.front();
  return 0;
}
void smain() {
  long long n, m;
  for (; cin >> n >> m;) {
    vector<pair<long long, long long>> a(n), b(m);
    for (long long i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    for (long long i = 0; i < m; i++) cin >> b[i].first >> b[i].second;
    cout << solve(a, b) << endl;
  }
}
