#include <bits/stdc++.h>
using namespace std;
vector<int> v, v2, v3, v4;
set<int> d, d2, dd;
set<int>::iterator it, it2, it3;
int main() {
  int n, m, c = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0;
  cin >> n >> m;
  vector<pair<int, int> > f(n);
  vector<pair<int, int> > s(m);
  for (int i = 0; i < n; i++) {
    cin >> f[i].first >> f[i].second;
  }
  for (int i = 0; i < m; i++) {
    cin >> s[i].first >> s[i].second;
  }
  for (int i = 0; i < n; i++) {
    d.clear();
    for (int j = 0; j < m; j++) {
      if (f[i].first == s[j].first && f[i].second != s[j].second) {
        d.insert(f[i].first);
        dd.insert(f[i].first);
      }
      if (f[i].first != s[j].first && f[i].second == s[j].second) {
        d.insert(f[i].second);
        dd.insert(f[i].second);
      }
      if (f[i].first == s[j].second && f[i].second != s[j].first) {
        d.insert(f[i].first);
        dd.insert(f[i].first);
      }
      if (f[i].first != s[j].second && f[i].second == s[j].first) {
        d.insert(f[i].second);
        dd.insert(f[i].second);
      }
    }
    if (d.size() > 1) return cout << -1, 0;
  }
  for (int i = 0; i < m; i++) {
    d2.clear();
    for (int j = 0; j < n; j++) {
      if (f[j].first == s[i].first && f[j].second != s[i].second) {
        d2.insert(s[i].first);
        dd.insert(s[i].first);
      }
      if (f[j].first != s[i].first && f[j].second == s[i].second) {
        d2.insert(s[i].second);
        dd.insert(s[i].second);
      }
      if (f[j].first == s[i].second && f[j].second != s[i].first) {
        d2.insert(s[i].second);
        dd.insert(s[i].second);
      }
      if (f[j].first != s[i].second && f[j].second == s[i].first) {
        d2.insert(s[i].first);
        dd.insert(s[i].first);
      }
    }
    if (d2.size() > 1) return cout << -1, 0;
  }
  if (dd.size() > 1) return cout << 0, 0;
  for (it3 = dd.begin(); it3 != dd.end(); it3++) cout << *it3 << "   ";
  cout << endl << endl;
}
