#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > one(n), two(m);
  for (int i = 0; i < n; ++i) {
    cin >> one[i].first >> one[i].second;
    if (one[i].first > one[i].second) swap(one[i].first, one[i].second);
  }
  for (int i = 0; i < m; ++i) {
    cin >> two[i].first >> two[i].second;
    if (two[i].first > two[i].second) swap(two[i].first, two[i].second);
  }
  set<int> answer;
  bool theyKnow = false, noOneKnows = false;
  for (int i = 0; i < n; ++i) {
    set<int> temp;
    for (int j = 0; j < m; ++j) {
      if (one[i].first == two[j].first && one[i].second == two[j].second) {
        continue;
      }
      if (one[i].first == two[j].first) {
        answer.insert(one[i].first);
        temp.insert(one[i].first);
      } else if (one[i].first == two[j].second) {
        answer.insert(one[i].first);
        temp.insert(one[i].first);
      } else if (one[i].second == two[j].first) {
        answer.insert(one[i].second);
        temp.insert(one[i].second);
      } else if (one[i].second == two[j].second) {
        answer.insert(one[i].second);
        temp.insert(one[i].second);
      }
    }
    if (temp.size() > 1) {
      noOneKnows = true;
    }
    if (temp.size() == 1) {
      theyKnow = true;
    }
  }
  for (int j = 0; j < m; ++j) {
    set<int> temp;
    for (int i = 0; i < n; ++i) {
      if (one[i].first == two[j].first && one[i].second == two[j].second) {
        continue;
      }
      if (one[i].first == two[j].first) {
        answer.insert(one[i].first);
        temp.insert(one[i].first);
      } else if (one[i].first == two[j].second) {
        answer.insert(one[i].first);
        temp.insert(one[i].first);
      } else if (one[i].second == two[j].first) {
        answer.insert(one[i].second);
        temp.insert(one[i].second);
      } else if (one[i].second == two[j].second) {
        answer.insert(one[i].second);
        temp.insert(one[i].second);
      }
    }
    if (temp.size() > 1) {
      noOneKnows = true;
    }
    if (temp.size() == 1) {
      theyKnow = true;
    }
  }
  if (answer.size() == 1) {
    cout << *answer.begin() << '\n';
    return 0;
  }
  if (noOneKnows) {
    cout << "-1\n";
    return 0;
  }
  if (theyKnow) {
    cout << "0\n";
    return 0;
  }
  return 0;
}
