#include <bits/stdc++.h>
using namespace std;
int common(pair<int, int> f, pair<int, int> s) {
  int tmp;
  if (f.first < f.second) {
    tmp = f.second;
    f.second = f.first;
    f.first = tmp;
  }
  if (s.first < s.second) {
    tmp = s.second;
    s.second = s.first;
    s.first = tmp;
  }
  if (f == s) return -1;
  if (f.first == s.first) return s.first;
  if (f.first == s.second) return s.second;
  if (f.second == s.first) return s.first;
  if (f.second == s.second) return s.second;
  return -1;
}
int solve(pair<int, int> *a, pair<int, int> *b, int n, int m) {
  set<int> ans, currans;
  int tmp;
  for (int i = 0; i < n; i++) {
    currans.erase(currans.begin(), currans.end());
    for (int j = 0; j < m; j++) {
      tmp = common(a[i], b[j]);
      if (tmp != -1) {
        ans.insert(tmp);
        currans.insert(tmp);
      }
    }
    if (currans.size() > 1) return -1;
  }
  if (ans.size() == 1)
    return (*ans.begin());
  else
    return 0;
}
int main() {
  int n, m;
  cin >> n >> m;
  pair<int, int> *f = new pair<int, int>[n];
  pair<int, int> *s = new pair<int, int>[m];
  for (int i = 0; i < n; i++) cin >> f[i].first >> f[i].second;
  for (int i = 0; i < m; i++) cin >> s[i].first >> s[i].second;
  int first = solve(f, s, n, m);
  int second = solve(s, f, m, n);
  if ((first == 0 && second == 1) || (first == 1 && second == 0) ||
      (first == 0 && second == 0))
    cout << 0;
  else if (first == -1 || second == -1)
    cout << -1;
  else
    cout << first;
  return 0;
}
