#include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <typename T>
using V = vector<T>;
template <typename T, typename S>
using P = pair<T, S>;
template <class T>
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
using LD = long double;
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') {
    cerr << *sdbg++;
  }
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
vector<pair<int, int>> a, b;
int possibleNumber(pair<int, int> a, pair<int, int> b) {
  map<int, int> m;
  for (int i : {a.first, a.second, b.first, b.second}) m[i]++;
  if (m.size() != 3) return -1;
  for (auto &i : m) {
    if (i.second == 2) return i.first;
  }
  return -1;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (0) cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  set<int> possible;
  for (int i = 0; i < n; i++) {
    map<int, int> M;
    for (int j = 0; j < m; j++) {
      auto &x = a[i];
      auto &y = b[j];
      auto ans = possibleNumber(x, y);
      if (ans != -1) {
        M[ans]++;
      }
    }
    if (!M.empty() && M.size() == 2) {
      cout << -1 << '\n';
      return 0;
    } else {
      (M.size());
      if (!M.empty()) possible.insert((M.begin())->first);
    }
  }
  for (int i = 0; i < m; i++) {
    map<int, int> M;
    for (int j = 0; j < n; j++) {
      auto &x = a[j];
      auto &y = b[i];
      auto ans = possibleNumber(x, y);
      if (ans != -1) {
        M[ans]++;
      }
    }
    if (!M.empty() && M.size() == 2) {
      cout << -1 << '\n';
      return 0;
    } else {
      (M.size());
      if (!M.empty()) possible.insert((M.begin())->first);
    }
  }
  if (possible.size() == 1) {
    cout << *(possible.begin()) << '\n';
    return 0;
  }
  if (possible.empty()) {
    cout << -1 << '\n';
  } else {
    cout << 0 << '\n';
  }
  if (0)
    cout << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
         << '\n';
  return 0;
}
