#include <bits/stdc++.h>
using namespace std;
struct pairhash {
 public:
  template <typename T, typename U>
  std::size_t operator()(const std::pair<T, U> &x) const {
    return std::hash<T>()(x.first) ^ (12345 + std::hash<U>()(x.second));
  }
};
bool equal(const pair<int, int> &l, const pair<int, int> &r) {
  return l.first == r.first && l.second == r.second ||
         l.first == r.second && l.second == r.first;
}
pair<int, bool> overlap(const pair<int, int> &l, const pair<int, int> &r) {
  if (r.first == l.first || r.second == l.first)
    return make_pair(l.first, true);
  if (r.first == l.second || r.second == l.second)
    return make_pair(l.second, false);
  return make_pair(0, false);
}
int main() {
  int m, n;
  cin >> m >> n;
  unordered_set<pair<int, int>, pairhash> st1, st2;
  set<int> common;
  for (int i = 0; i < m; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    st1.emplace(t1, t2);
  }
  for (int i = 0; i < n; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    st2.emplace(t1, t2);
  }
  int count = 0, candidate = 0;
  bool they_can_deduce = true;
  for (auto &pa1 : st1) {
    bool first_used = false, second_used = false;
    for (auto &pa2 : st2) {
      if (equal(pa1, pa2)) continue;
      auto over = overlap(pa1, pa2);
      if (over.first) {
        count++;
        candidate = over.first;
        common.insert(over.first);
        if (over.second)
          first_used = true;
        else
          second_used = true;
      }
    }
    if (first_used && second_used) {
      they_can_deduce = false;
    }
  }
  if (count == 1) {
    cout << candidate << endl;
    return 0;
  }
  if (common.size() == 1) {
    cout << *common.begin() << endl;
    return 0;
  }
  common.clear();
  count = 0;
  for (auto &pa1 : st2) {
    bool first_used = false, second_used = false;
    for (auto &pa2 : st1) {
      if (equal(pa1, pa2)) continue;
      auto over = overlap(pa1, pa2);
      if (over.first) {
        count++;
        candidate = over.first;
        common.insert(over.first);
        if (over.second)
          first_used = true;
        else
          second_used = true;
      }
    }
    if (first_used && second_used) {
      they_can_deduce = false;
    }
  }
  if (count == 1) {
    cout << candidate << endl;
    return 0;
  }
  if (common.size() == 1) {
    cout << *common.begin() << endl;
    return 0;
  }
  common.clear();
  if (they_can_deduce) {
    cout << 0 << endl;
  } else
    cout << -1 << endl;
  return 0;
}
