#include <bits/stdc++.h>
using namespace std;
long long dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long dy[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << "\n"; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
void solve(long long tt) {
  long long n;
  cin >> n;
  long long m;
  cin >> m;
  vector<pair<long long, long long> > arr1(n), arr2(m);
  for (auto &a : arr1) cin >> a.first >> a.second;
  for (auto &b : arr2) cin >> b.first >> b.second;
  set<long long> cm;
  long long tr = -20;
  for (auto a : arr1) {
    long long cnt1 = 0, cnt2 = 0;
    for (auto b : arr2) {
      if (a.first == b.first && a.second != b.second) {
        tr = a.first;
        cnt1++;
      }
      if (a.first == b.second && a.second != b.first) {
        tr = a.first;
        cnt1++;
      }
      if (a.second == b.second && a.first != b.first) {
        tr = a.second;
        cnt2++;
      }
      if (a.second == b.first && a.first != b.second) {
        tr = a.second;
        cnt2++;
      }
    };
    if (min(cnt1, cnt2) > 0) {
      cout << -1 << "\n";
      return;
    }
    if (cnt1 && !cnt2) {
      cm.insert(tr);
    }
    if (!cnt1 && cnt2) {
      cm.insert(tr);
    }
  }
  set<long long> cm2;
  for (auto a : arr2) {
    long long cnt1 = 0, cnt2 = 0;
    for (auto b : arr1) {
      if (a.first == b.first && a.second != b.second) {
        tr = a.first;
        cnt1++;
      }
      if (a.first == b.second && a.second != b.first) {
        tr = a.first;
        cnt1++;
      }
      if (a.second == b.second && a.first != b.first) {
        tr = a.second;
        cnt2++;
      }
      if (a.second == b.first && a.first != b.second) {
        tr = a.second;
        cnt2++;
      }
    }
    if (min(cnt1, cnt2) > 0) {
      cout << -1 << "\n";
      return;
    }
    if (cnt1 && !cnt2) {
      cm2.insert(tr);
    }
    if (!cnt1 && cnt2) {
      cm2.insert(tr);
    }
  };
  if (cm.empty() || cm2.empty())
    cout << -1 << "\n";
  else if (cm.size() == 1)
    cout << *cm.begin() << "\n";
  else
    cout << 0 << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long nn = 1;
  for (long long i = 0; i < (long long)nn; i++) solve(i + 1);
}
