#include <bits/stdc++.h>
using namespace std;
pair<int, int> firstP[15];
pair<int, int> secondP[15];
set<int> good;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> firstP[i].first >> firstP[i].second;
  for (int i = 0; i < m; i++) cin >> secondP[i].first >> secondP[i].second;
  for (int i = 0; i < n; i++) {
    bool f1 = false;
    bool f2 = false;
    for (int j = 0; j < m; j++) {
      if (firstP[i] == secondP[j] || (firstP[i].first == secondP[j].second &&
                                      firstP[i].second == secondP[j].first))
        continue;
      vector<int> G1;
      G1.push_back(secondP[j].first);
      G1.push_back(secondP[j].second);
      if (find(G1.begin(), G1.end(), firstP[i].first) != G1.end() &&
          find(G1.begin(), G1.end(), firstP[i].second) == G1.end())
        f1 = true;
      if (find(G1.begin(), G1.end(), firstP[i].first) == G1.end() &&
          find(G1.begin(), G1.end(), firstP[i].second) != G1.end())
        f2 = true;
    }
    if (f1 && f2) {
      cout << "-1";
      return 0;
    }
    if (f1) good.insert(firstP[i].first);
    if (f2) good.insert(firstP[i].second);
  }
  for (int i = 0; i < max(n, m); i++) swap(firstP[i], secondP[i]);
  swap(n, m);
  for (int i = 0; i < n; i++) {
    bool f1 = false;
    bool f2 = false;
    for (int j = 0; j < m; j++) {
      if (firstP[i] == secondP[j] || (firstP[i].first == secondP[j].second &&
                                      firstP[i].second == secondP[j].first))
        continue;
      vector<int> G1;
      G1.push_back(secondP[j].first);
      G1.push_back(secondP[j].second);
      if (find(G1.begin(), G1.end(), firstP[i].first) != G1.end() &&
          find(G1.begin(), G1.end(), firstP[i].second) == G1.end())
        f1 = true;
      if (find(G1.begin(), G1.end(), firstP[i].first) == G1.end() &&
          find(G1.begin(), G1.end(), firstP[i].second) != G1.end())
        f2 = true;
    }
    if (f1 && f2) {
      cout << "-1";
      return 0;
    }
    if (f1) good.insert(firstP[i].first);
    if (f2) good.insert(firstP[i].second);
  }
  if (good.size() >= 2) {
    cout << 0;
    return 0;
  }
  cout << *good.begin();
  return 0;
}
