#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[55], b[55];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
  for (int j = 0; j < m; j++) cin >> b[j].first >> b[j].second;
  set<int> diff;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int cnt = 0;
      int who = -1;
      if (a[i].first == b[j].first) cnt++, who = a[i].first;
      if (a[i].second == b[j].first) cnt++, who = a[i].second;
      if (a[i].first == b[j].second) cnt++, who = a[i].first;
      if (a[i].second == b[j].second) cnt++, who = a[i].second;
      if (cnt == 1) {
        diff.insert(who);
      }
    }
  }
  if (diff.size() == 1) {
    cout << *diff.begin() << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    set<int> fir;
    for (int j = 0; j < m; j++) {
      int cnt = 0;
      int who = -1;
      if (a[i].first == b[j].first) cnt++, who = a[i].first;
      if (a[i].second == b[j].first) cnt++, who = a[i].second;
      if (a[i].first == b[j].second) cnt++, who = a[i].first;
      if (a[i].second == b[j].second) cnt++, who = a[i].second;
      if (cnt == 1) {
        fir.insert(who);
      }
    }
    if (fir.size() > 1) {
      cout << "-1" << endl;
      return 0;
    }
  }
  for (int j = 0; j < m; j++) {
    set<int> fir;
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      int who = -1;
      if (a[i].first == b[j].first) cnt++, who = a[i].first;
      if (a[i].second == b[j].first) cnt++, who = a[i].second;
      if (a[i].first == b[j].second) cnt++, who = a[i].first;
      if (a[i].second == b[j].second) cnt++, who = a[i].second;
      if (cnt == 1) {
        fir.insert(who);
      }
    }
    if (fir.size() > 1) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << "0" << endl;
  return 0;
}
