#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9;
const double eps = (1e-10);
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
int dcmp(double x, double y) { return fabs(x - y) <= eps ? 0 : x < y ? -1 : 1; }
int n, m, x, y;
set<pair<int, int> > A, B;
set<int> cands, local_cands;
int main() {
  cin >> n >> m;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> x >> y;
    A.insert(make_pair(x, y));
  }
  for (int i = 0; i < (int)(m); ++i) {
    cin >> x >> y;
    B.insert(make_pair(x, y));
  }
  for (auto& p1 : A) {
    local_cands.clear();
    for (auto& p2 : B) {
      map<int, int> freq;
      int cand = 0;
      ++freq[p1.first], ++freq[p1.second], ++freq[p2.first], ++freq[p2.second];
      if (((int)((freq).size())) == 3)
        for (auto& f : freq)
          if (f.second == 2) cand = f.first;
      if (cand) {
        cands.insert(cand), local_cands.insert(cand);
      }
    }
    if (((int)((local_cands).size())) > 1) {
      cout << -1;
      return 0;
    }
  }
  if (((int)((cands).size())) == 1) {
    cout << *cands.begin();
    return 0;
  }
  for (auto& p1 : B) {
    local_cands.clear();
    for (auto& p2 : A) {
      map<int, int> freq;
      int cand = 0;
      ++freq[p1.first], ++freq[p1.second], ++freq[p2.first], ++freq[p2.second];
      if (((int)((freq).size())) == 3)
        for (auto& f : freq)
          if (f.second == 2) cand = f.first;
      if (cand) local_cands.insert(cand);
    }
    if (((int)((local_cands).size())) > 1) {
      cout << -1;
      return 0;
    }
  }
  cout << 0;
  return 0;
}
