#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const int MAX_N = 3 * 1e5 + 1;
const long long INF = 1e9;
const long long delta = 10957;
using namespace std;
long long check(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first == p2.first and p1.second != p2.second) return p1.first;
  if (p1.first != p2.first and p1.second == p2.second) return p2.second;
  if (p1.first == p2.second and p1.second != p2.first) return p1.first;
  if (p1.first != p2.second and p1.second == p2.first) return p1.second;
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cout.tie();
  cin.tie();
  int n, m;
  cin >> n >> m;
  pair<int, int> p1[n], p2[m];
  for (int i = 0; i < n; ++i) cin >> p1[i].first >> p1[i].second;
  for (int i = 0; i < m; ++i) cin >> p2[i].first >> p2[i].second;
  set<int> st;
  for (int i = 0; i < n; ++i) {
    set<int> fs;
    for (int j = 0; j < m; ++j) {
      if (check(p1[i], p2[j])) {
        fs.insert(check(p1[i], p2[j]));
        st.insert(check(p1[i], p2[j]));
      }
    }
    if (fs.size() > 1) return cout << -1, 0;
  }
  for (int i = 0; i < m; ++i) {
    set<int> fs;
    for (int j = 0; j < n; ++j) {
      if (check(p2[i], p1[j])) {
        fs.insert(check(p2[i], p1[j]));
        st.insert(check(p2[i], p1[j]));
      }
    }
    if (fs.size() > 1) return cout << -1, 0;
  }
  if (st.size() == 1)
    cout << *st.begin();
  else
    cout << 0;
  return 0;
}
