#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}
int calc(pair<int, int> pa, pair<int, int> pb) {
  if (pa > pb) swap(pa, pb);
  if (pa.first == pb.first) {
    if (pa.second != pb.second)
      return pa.first;
    else
      return -1;
  }
  if (pa.second == pb.first) return pa.second;
  if (pa.second == pb.second) return pa.second;
  return -1;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a, b;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    a.push_back({x, y});
  }
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    b.push_back({x, y});
  }
  set<int> ansSum;
  for (int i = 0; i < n; ++i) {
    set<int> ans;
    for (int j = 0; j < m; ++j) {
      int res = calc(a[i], b[j]);
      if (res >= 0) {
        ans.insert(res);
        ansSum.insert(res);
      }
    }
    if (ans.size() > 1) {
      cout << "-1" << endl;
      return;
    };
  }
  for (int j = 0; j < m; ++j) {
    set<int> ans;
    for (int i = 0; i < n; ++i) {
      int res = calc(a[i], b[j]);
      if (res >= 0) {
        ans.insert(res);
        ansSum.insert(res);
      }
    }
    if (ans.size() > 1) {
      cout << "-1" << endl;
      return;
    };
  }
  if (ansSum.size() == 1)
    cout << *ansSum.begin();
  else if (ansSum.size() > 1)
    cout << 0;
  else
    cout << -1;
  cout << endl;
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}
