#include <bits/stdc++.h>
using namespace std;
using Vote = std::pair<int, int>;
struct pair_hash {
  template <class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2> &p) const {
    auto h1 = std::hash<T1>{}(p.first);
    auto h2 = std::hash<T2>{}(p.second);
    return h1 * 100 + h2;
  }
};
unordered_map<Vote, int, pair_hash> _firstMap;
unordered_map<Vote, int, pair_hash> _secondMap;
int share(pair<int, int> first, pair<int, int> second) {
  if (first == second) return 0;
  if (first.first == second.second && first.second == second.first) return 0;
  if (first.first == second.first || first.first == second.second)
    return first.first;
  if (first.second == second.first || first.second == second.second)
    return first.second;
  return -1;
}
int solve(vector<pair<int, int>> first, vector<pair<int, int>> second) {
  int res = -1;
  set<int> resSet;
  resSet.clear();
  for (pair<int, int> p : first) {
    for (pair<int, int> s : second) {
      int i = share(p, s);
      if (i > 0) {
        if (_firstMap[p] != NULL && _firstMap[p] != i) {
          return -1;
        } else {
          _firstMap[p] = i;
          res = i;
          if (resSet.find(i) == resSet.end()) {
            resSet.insert(i);
          }
        }
      }
    }
  }
  for (pair<int, int> p : second) {
    for (pair<int, int> s : first) {
      int i = share(p, s);
      if (i > 0) {
        if (_secondMap[p] != NULL && _secondMap[p] != i) {
          return -1;
        } else {
          _secondMap[p] = i;
          res = i;
          if (resSet.find(i) == resSet.end()) {
            resSet.insert(i);
          }
        }
      }
    }
  }
  if ((_firstMap.size() > 1 || _secondMap.size() > 1) && resSet.size() > 1)
    return 0;
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> first(n);
  vector<pair<int, int>> second(m);
  _firstMap.clear();
  _secondMap.clear();
  for (int i = 0; i < n; i++) {
    cin >> first[i].first >> first[i].second;
  }
  for (int i = 0; i < m; i++) {
    cin >> second[i].first >> second[i].second;
  }
  cout << solve(first, second) << "\n";
  return 0;
}
