#include <bits/stdc++.h>
using namespace std;
set<int> S;
int fuck(vector<pair<int, int> > &p1, vector<pair<int, int> > &p2, int n,
         int m) {
  for (int i = 0; i < n; i += 1) {
    bool ans1 = false, ans2 = false;
    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)
        ans1 = true;
      if (p1[i].second == p2[j].first || p1[i].second == p2[j].second)
        ans2 = true;
    }
    if (ans1 && ans2) return -1;
    if (ans1) S.insert(p1[i].first);
    if (ans2) S.insert(p1[i].second);
  }
  return 0;
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<pair<int, int> > p1(n);
  vector<pair<int, int> > p2(m);
  for (int i = 0; i < n; i += 1) {
    cin >> x >> y;
    p1[i].first = min(x, y), p1[i].second = max(x, y);
  }
  for (int i = 0; i < m; i += 1) {
    cin >> x >> y;
    p2[i].first = min(x, y), p2[i].second = max(x, y);
  }
  int res = fuck(p1, p2, n, m);
  if (res == -1) {
    cout << "-1\n";
    return 0;
  }
  res = fuck(p2, p1, m, n);
  if (res == -1) {
    cout << "-1\n";
    return 0;
  }
  if (S.size() > 1)
    cout << 0 << '\n';
  else
    cout << (*(S.begin())) << '\n';
}
