#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
vector<pair<int, int>> A, B;
int match(pair<int, int> x, pair<int, int> y) {
  if (x.first == y.second) return x.first;
  if (x.first == y.first && x.second != y.second) return x.first;
  if (x.second == y.first) return x.second;
  if (x.second == y.second && x.first != y.first) return x.second;
  return 0;
}
void Input() {
  cin >> n >> m;
  A.resize(n);
  B.resize(m);
  for (auto &P : A) {
    cin >> P.first >> P.second;
    if (P.first > P.second) swap(P.first, P.second);
  }
  for (auto &P : B) {
    cin >> P.first >> P.second;
    if (P.first > P.second) swap(P.first, P.second);
  }
}
void Solve() {
  set<int> Possibility;
  for (int i = 0; i < n; i++) {
    set<int> PossibilitySubset;
    for (int j = 0; j < m; j++) {
      if (!match(A[i], B[j])) continue;
      PossibilitySubset.insert(match(A[i], B[j]));
    }
    if (PossibilitySubset.empty()) continue;
    if (PossibilitySubset.size() > 1) {
      cout << "-1\n";
      return;
    }
    Possibility.insert(*PossibilitySubset.begin());
  }
  for (int j = 0; j < m; j++) {
    set<int> PossibilitySubset;
    for (int i = 0; i < n; i++) {
      if (!match(A[i], B[j])) continue;
      PossibilitySubset.insert(match(A[i], B[j]));
    }
    if (PossibilitySubset.empty()) continue;
    if (PossibilitySubset.size() > 1) {
      cout << "-1\n";
      return;
    }
    Possibility.insert(*PossibilitySubset.begin());
  }
  if (Possibility.empty())
    cout << "-1\n";
  else if (Possibility.size() > 1)
    cout << "0\n";
  else
    cout << (*Possibility.begin()) << '\n';
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  Input();
  Solve();
  return 0;
}
