#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    int n, m, i, j, k, num, x, y;
    cin >> n >> m;
    vector<pair<int, int> > pehla, doosra;
    for (i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      pehla.push_back(make_pair(x, y));
    }
    for (i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      doosra.push_back(make_pair(x, y));
    }
    int flag1 = 1, flag2 = 1;
    vector<set<int> > fshd(n);
    vector<set<int> > sshd(m);
    for (i = 0; i < n; i++) {
      x = pehla[i].first, y = pehla[i].second;
      for (j = 0; j < m; j++) {
        int x1 = doosra[j].first, y1 = doosra[j].second;
        if ((x == x1 && y != y1) || (x == y1 && y != x1) ||
            (y == x1 && x != y1) || (y == y1 && x != x1)) {
          if (x == x1 || x == y1)
            fshd[i].insert(x);
          else
            fshd[i].insert(y);
        }
      }
      if ((long long int)fshd[i].size() > 1) {
        flag1 = 0;
        break;
      }
    }
    for (i = 0; i < m; i++) {
      x = doosra[i].first, y = doosra[i].second;
      for (j = 0; j < n; j++) {
        int x1 = pehla[j].first, y1 = pehla[j].second;
        if ((x == x1 && y != y1) || (x == y1 && y != x1) ||
            (y == x1 && x != y1) || (y == y1 && x != x1)) {
          if (x == x1 || x == y1)
            sshd[i].insert(x);
          else
            sshd[i].insert(y);
        }
      }
      if ((long long int)sshd[i].size() > 1) {
        flag2 = 0;
        break;
      }
    }
    if (flag1 == 0 || flag2 == 0) {
      cout << "-1";
      return 0;
    } else {
      set<int> final;
      for (i = 0; i < n; i++) {
        if ((long long int)fshd[i].size()) final.insert(*fshd[i].begin());
      }
      for (i = 0; i < m; i++) {
        if ((long long int)sshd[i].size()) final.insert(*sshd[i].begin());
      }
      if ((long long int) final.size() == 1)
        cout << *final.begin();
      else
        cout << "0";
    }
  }
  return 0;
}
