#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > f(n), s(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;
  }
  set<int> pos;
  bool know = true;
  for (int i = 0; i < n; ++i) {
    set<int> cpos;
    for (int j = 0; j < m; ++j) {
      if (f[i].first != s[j].first and f[i].second == s[j].second) {
        pos.insert(f[i].second);
        cpos.insert(f[i].second);
      }
      if (f[i].first != s[j].second and f[i].second == s[j].first) {
        pos.insert(f[i].second);
        cpos.insert(f[i].second);
      }
      if (f[i].first == s[j].first and f[i].second != s[j].second) {
        pos.insert(f[i].first);
        cpos.insert(f[i].first);
      }
      if (f[i].first == s[j].second and f[i].second != s[j].first) {
        pos.insert(f[i].first);
        cpos.insert(f[i].first);
      }
    }
    if (cpos.size() > 1) know = false;
  }
  for (int j = 0; j < m; ++j) {
    set<int> cpos;
    for (int i = 0; i < n; ++i) {
      if (f[i].first != s[j].first and f[i].second == s[j].second) {
        pos.insert(f[i].second);
        cpos.insert(f[i].second);
      }
      if (f[i].first != s[j].second and f[i].second == s[j].first) {
        pos.insert(f[i].second);
        cpos.insert(f[i].second);
      }
      if (f[i].first == s[j].first and f[i].second != s[j].second) {
        pos.insert(f[i].first);
        cpos.insert(f[i].first);
      }
      if (f[i].first == s[j].second and f[i].second != s[j].first) {
        pos.insert(f[i].first);
        cpos.insert(f[i].first);
      }
    }
    if (cpos.size() > 1) know = false;
  }
  if (pos.size() == 1)
    cout << *pos.begin() << endl;
  else if (know)
    cout << 0 << endl;
  else
    cout << -1 << endl;
}
