#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000LL;
const long long NMAX = 1e6 + 4;
const long long mod = 1e9 + 7;
const double eps = 1e-10;
const double PI = acos(-1);
vector<pair<long long, long long> > pairs1, pairs2;
long long N, M;
set<long long> solns1[14];
set<long long> solns2[14];
long long match(pair<long long, long long> a, pair<long long, long long> b) {
  if (a == b) return -1;
  if (a.first == b.first || a.first == b.second) return a.first;
  if (a.second == b.first || a.second == b.second) return a.second;
  return -1;
}
signed main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  pairs1.assign(N, {0, 0});
  pairs2.assign(M, {0, 0});
  for (long long i = 0; i < N; i++) {
    long long a, b;
    cin >> a >> b;
    pairs1[i].first = min(a, b);
    pairs1[i].second = max(a, b);
  }
  for (long long i = 0; i < M; i++) {
    long long a, b;
    cin >> a >> b;
    pairs2[i].first = min(a, b);
    pairs2[i].second = max(a, b);
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < M; j++) {
      long long k = match(pairs1[i], pairs2[j]);
      if (k != -1) solns1[i].insert(k);
    }
  }
  for (long long i = 0; i < M; i++) {
    for (long long j = 0; j < N; j++) {
      long long k = match(pairs2[i], pairs1[j]);
      if (k != -1) solns2[i].insert(k);
    }
  }
  for (long long i = 0; i < N; i++) {
    if (solns1[i].size() > 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (long long i = 0; i < M; i++) {
    if (solns2[i].size() > 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  long long soln = -1;
  for (long long i = 0; i < N; i++) {
    for (long long k : solns1[i]) {
      if (soln != -1 and k != soln) {
        cout << 0 << endl;
        return 0;
      }
      soln = k;
    }
  }
  for (long long i = 0; i < M; i++) {
    for (long long k : solns2[i]) {
      if (soln != -1 and k != soln) {
        cout << 0 << endl;
        return 0;
      }
      soln = k;
    }
  }
  cout << soln << endl;
}
