#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[15], b[15];
set<int> lmao;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  for (int i = 0; i < n; i++) {
    set<int> buh;
    for (int j = 0; j < m; j++) {
      if (!(a[i].first == b[j].first && a[i].second == b[j].second) &&
          !(a[i].first == b[j].second && a[i].second == b[j].first)) {
        if (a[i].first == b[j].second || a[i].first == b[j].first)
          buh.insert(a[i].first);
        else if (a[i].second == b[j].first || a[i].second == b[j].second)
          buh.insert(a[i].second);
      }
    }
    if (buh.size() == 1) {
      lmao.insert(*buh.begin());
    } else if (buh.size() == 2) {
      cout << -1;
      return 0;
    }
  }
  for (int j = 0; j < m; j++) {
    set<int> buh;
    for (int i = 0; i < n; i++) {
      if (!(a[i].first == b[j].first && a[i].second == b[j].second) &&
          !(a[i].first == b[j].second && a[i].second == b[j].first)) {
        if (a[i].first == b[j].second || a[i].first == b[j].first)
          buh.insert(a[i].first);
        else if (a[i].second == b[j].first || a[i].second == b[j].second)
          buh.insert(a[i].second);
      }
    }
    if (buh.size() == 2) {
      cout << -1;
      return 0;
    }
  }
  if (lmao.size() == 1)
    cout << *lmao.begin();
  else
    cout << 0;
}
