#include <bits/stdc++.h>
using namespace std;
bool less_vectors(const vector<long long>& a, const vector<long long>& b) {
  return a.size() < b.size();
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > v1(n);
  vector<pair<long long, long long> > v2(m);
  vector<long long> block1(n, 0);
  vector<long long> block2(m, 0);
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    v1[i] = (make_pair(a, b));
  }
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    v2[i] = make_pair(a, b);
  }
  long long ans = -1;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (v1[i].first == v2[j].first && v1[i].second == v2[j].second) {
        block1[i] = 0;
        block2[j] = 0;
      }
    }
  }
  long long cminus1 = 0;
  for (long long i = 0; i < n; i++) {
    if (block1[i]) continue;
    long long ch1 = 0;
    long long ch2 = 0;
    for (long long j = 0; j < m; j++) {
      if (block2[j]) continue;
      if (v1[i].first == v2[j].first && v1[i].second == v2[j].second) {
        continue;
      }
      if (v2[j].first == v1[i].first || v2[j].second == v1[i].first) ch1 = 1;
      if (v2[j].first == v1[i].second || v2[j].second == v1[i].second) ch2 = 1;
    }
    if (ch1 && ch2) cminus1 = 1;
  }
  long long cminus2 = 0;
  for (long long i = 0; i < m; i++) {
    if (block2[i]) continue;
    long long ch1 = 0;
    long long ch2 = 0;
    for (long long j = 0; j < n; j++) {
      if (block1[j]) continue;
      if (v2[i].first == v1[j].first && v2[i].second == v1[j].second) {
        continue;
      }
      if (v1[j].first == v2[i].first || v1[j].second == v2[i].first) ch1 = 1;
      if (v1[j].first == v2[i].second || v1[j].second == v2[i].second) ch2 = 1;
    }
    if (ch1 && ch2) cminus2 = 1;
  }
  long long cnt = 0;
  long long no = 0;
  for (long long i = 0; i < n; i++) {
    if (block1[i]) continue;
    long long same = 0;
    for (long long j = 0; j < m; j++) {
      if (block2[j]) continue;
      if (v1[i].first == v2[j].first && v1[i].second == v2[j].second) {
        continue;
      }
      if (v1[i].first == v2[j].first || v1[i].first == v2[j].second) same = 1;
    }
    if (same && no != v1[i].first) {
      cnt++;
      no = v1[i].first;
    }
    same = 0;
    for (long long j = 0; j < m; j++) {
      if (block2[j]) continue;
      if (v1[i].first == v2[j].first && v1[i].second == v2[j].second) {
        continue;
      }
      if (v1[i].second == v2[j].first || v1[i].second == v2[j].second) same = 1;
    }
    if (same && no != v1[i].second) {
      cnt++;
      no = v1[i].second;
    }
  }
  if (cminus1 || cminus2)
    cout << -1;
  else if (cnt > 1)
    cout << 0;
  else if (cnt == 1)
    cout << no;
  return 0;
}
