#include <bits/stdc++.h>
using namespace std;
bool cal(set<int> &pc, int n, int m, vector<pair<int, int>> &p1,
         vector<pair<int, int>> &p2) {
  bool flag = true;
  for (int i = 0; i < n && flag; i += 1) {
    set<int> common;
    for (int j = 0; j < m; j += 1) {
      if (p1[i] == p2[j]) continue;
      if (p1[i].first == p2[j].first || p1[i].first == p2[j].second)
        common.insert(p1[i].first);
      else if (p1[i].second == p2[j].first || p1[i].second == p2[j].second)
        common.insert(p1[i].second);
    }
    if (common.size() == 1)
      pc.insert(*(common.begin()));
    else if (common.size() > 1)
      flag = false;
  }
  return flag;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> p1(n);
  for (int i = 0; i < n; i += 1) {
    cin >> p1[i].first >> p1[i].second;
    if (p1[i].first > p1[i].second) swap(p1[i].first, p1[i].second);
  }
  vector<pair<int, int>> p2(m);
  for (int i = 0; i < m; i += 1) {
    cin >> p2[i].first >> p2[i].second;
    if (p2[i].first > p2[i].second) swap(p2[i].first, p2[i].second);
  }
  set<int> pc1, pc2;
  if (cal(pc1, n, m, p1, p2) && cal(pc2, m, n, p2, p1)) {
    if (pc1.size() == 1)
      cout << *(pc1.begin()) << endl;
    else
      cout << 0 << endl;
  } else {
    cout << -1 << endl;
  }
}
