#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, n, m, x, y;
  vector<pair<long long, long long> > possible_second, possible_first;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    possible_first.push_back({x, y});
  }
  map<pair<long long, long long>, set<long long> > mm, nn;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    possible_second.push_back({x, y});
  }
  long long le = 0;
  set<long long> ppo;
  for (auto it : possible_first) {
    for (auto iit : possible_second) {
      set<long long> ss;
      long long common;
      ss.insert(it.first);
      ss.insert(it.second);
      if (ss.find(iit.first) != ss.end()) {
        common = iit.first;
      }
      ss.insert(iit.first);
      if (ss.find(iit.second) != ss.end()) {
        common = iit.second;
      }
      ss.insert(iit.second);
      if (ss.size() == 3) {
        mm[it].insert(common);
        ppo.insert(common);
      }
    }
  }
  for (auto it : possible_second) {
    for (auto iit : possible_first) {
      set<long long> ss;
      long long common;
      ss.insert(it.first);
      ss.insert(it.second);
      if (ss.find(iit.first) != ss.end()) {
        common = iit.first;
      }
      ss.insert(iit.first);
      if (ss.find(iit.second) != ss.end()) {
        common = iit.second;
      }
      ss.insert(iit.second);
      if (ss.size() == 3) {
        nn[it].insert(common);
      }
    }
  }
  if (ppo.size() == 1) {
    cout << (*ppo.begin());
    return 0;
  }
  bool pakka = 1;
  for (auto it : mm) {
    if (it.second.size() > 1) {
      pakka = 0;
      break;
    }
  }
  if (pakka) {
    for (auto it : nn) {
      if (it.second.size() > 1) {
        pakka = 0;
        break;
      }
    }
  }
  if (pakka) {
    cout << 0;
    return 0;
  }
  cout << -1;
  return 0;
}
