#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> v1;
  vector<pair<int, int>> v2;
  vector<pair<int, int>> result;
  std::vector<int> vvv;
  std::vector<int> tt;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    v1.emplace_back(a, b);
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v2.emplace_back(a, b);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int a1 = v1[i].first, b1 = v1[i].second;
      int a2 = v2[j].first, b2 = v2[j].second;
      if (a1 > b1) {
        swap(a1, b1);
      }
      if (a2 > b2) {
        swap(a2, b2);
      }
      if (a1 == a2 && b1 == b2) {
        continue;
      } else if (a1 == a2) {
        result.emplace_back(i, j);
        vvv.push_back(a1);
        tt.push_back(a1);
      } else if (b1 == b2) {
        result.emplace_back(i, j);
        vvv.push_back(b1);
        tt.push_back(b1);
      } else if (a1 == b2) {
        result.emplace_back(i, j);
        vvv.push_back(a1);
        tt.push_back(a1);
      } else if (b1 == a2) {
        result.emplace_back(i, j);
        vvv.push_back(b1);
        tt.push_back(b1);
      }
    }
  }
  std::sort(vvv.begin(), vvv.end());
  auto last = std::unique(vvv.begin(), vvv.end());
  vvv.erase(last, vvv.end());
  if (vvv.size() == 1) {
    cout << vvv[0] << endl;
    return 0;
  }
  std::vector<vector<int>> vv1(n);
  std::vector<vector<int>> vv2(m);
  for (size_t i = 0; i < result.size(); i++) {
    for (size_t j = i + 1; j < result.size(); j++) {
      if (result[i].first == result[j].first) {
        vv1[result[i].first].push_back(tt[i]);
        vv1[result[i].first].push_back(tt[j]);
      }
      if (result[i].second == result[j].second) {
        vv2[result[i].second].push_back(tt[i]);
        vv2[result[i].second].push_back(tt[j]);
      }
    }
  }
  for (size_t i = 0; i < vv1.size(); i++) {
    auto tmp = vv1[i];
    std::sort(tmp.begin(), tmp.end());
    auto last = std::unique(tmp.begin(), tmp.end());
    tmp.erase(last, tmp.end());
    if (tmp.size() > 1) {
      cout << "-1\n";
      return 0;
    }
  }
  for (size_t i = 0; i < vv2.size(); i++) {
    auto tmp = vv2[i];
    std::sort(tmp.begin(), tmp.end());
    auto last = std::unique(tmp.begin(), tmp.end());
    tmp.erase(last, tmp.end());
    if (tmp.size() > 1) {
      cout << "-1\n";
      return 0;
    }
  }
  cout << "0";
  return 0;
}
