#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double pi = 2 * acos(0.0);
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  pair<long long, long long> fool[n];
  pair<long long, long long> baar[m];
  for (long long i = 0; i < n; i++) {
    cin >> fool[i].first >> fool[i].second;
  }
  for (long long i = 0; i < m; i++) {
    cin >> baar[i].first >> baar[i].second;
  }
  pair<long long, long long> p1;
  pair<long long, long long> p2;
  map<pair<long long, long long>, pair<pair<long long, long long>, long long>>
      mp1;
  map<pair<long long, long long>, pair<pair<long long, long long>, long long>>
      mp2;
  long long common;
  bool fnd;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      fnd = false;
      p1 = fool[i];
      p2 = baar[j];
      if (p1.second < p1.first) {
        long long temp = p1.first;
        p1.first = p1.second;
        p1.second = temp;
      }
      if (p2.second < p2.first) {
        long long temp = p2.first;
        p2.first = p2.second;
        p2.second = temp;
      }
      if (p1 == p2) {
        continue;
      }
      if (p1.first == p2.first || p1.first == p2.second) {
        common = p1.first;
        fnd = true;
      } else if (p1.second == p2.second || p1.second == p2.first) {
        common = p1.second;
        fnd = true;
      }
      if (fnd) {
        if (mp1.count(p1) == 0) {
          mp1[p1] = make_pair(p2, common);
        } else if (mp1[p1].second != common) {
          cout << "-1\n";
          return 0;
        }
        if (mp2.count(p2) == 0) {
          mp2[p2] = make_pair(p1, common);
        } else if (mp2[p2].second != common) {
          cout << "-1\n";
          return 0;
        }
      }
    }
  }
  long long res = INT_MAX;
  pair<pair<long long, long long>, long long> p;
  for (auto it = mp1.begin(); it != mp1.end(); ++it) {
    p = it->second;
    if (res == INT_MAX) {
      res = p.second;
    } else if (p.second != res) {
      cout << "0\n";
      return 0;
    }
  }
  for (auto it = mp2.begin(); it != mp2.end(); ++it) {
    p = it->second;
    if (res == INT_MAX) {
      res = p.second;
    } else if (p.second != res) {
      cout << "0\n";
      return 0;
    }
  }
  cout << res << "\n";
  return 0;
}
