#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
int n, m;
bool andan[15];
pair<int, int> F[15], S[15];
int matchme(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first && a.second == b.second) return 0;
  if (a.first == b.first) return a.first;
  if (a.first == b.second) return a.first;
  if (a.second == b.first) return a.second;
  if (a.second == b.second) return a.second;
  return 0;
}
int main() {
  while (scanf("%d %d", &n, &m) >= 1) {
    bool win = true, crap = false;
    for (int i = (0); i < (int)(15); i++) andan[i] = false;
    for (int i = (0); i < (int)(n); i++) {
      scanf("%lld %lld", &F[i].first, &F[i].second);
      if (F[i].first > F[i].second) swap(F[i].first, F[i].second);
    }
    for (int i = (0); i < (int)(m); i++) {
      scanf("%lld %lld", &S[i].first, &S[i].second);
      if (S[i].first > S[i].second) swap(S[i].first, S[i].second);
    }
    for (int i = (0); i < (int)(n); i++) {
      bool ml = false, mr = false;
      for (int j = (0); j < (int)(m); j++) {
        int val = matchme(F[i], S[j]);
        if (!val) continue;
        if (val == F[i].first) ml = true;
        if (val == F[i].second) mr = true;
        andan[val] = true;
      }
      if (ml && mr) crap = true;
    }
    for (int j = (0); j < (int)(m); j++) {
      bool ml = false, mr = false;
      for (int i = (0); i < (int)(n); i++) {
        int val = matchme(F[i], S[j]);
        if (!val) continue;
        if (val == S[j].first) ml = true;
        if (val == S[j].second) mr = true;
        andan[val] = true;
      }
      if (ml && mr) crap = true;
    }
    int cnt = 0, ans = -1;
    for (int i = (0); i < (int)(15); i++)
      if (andan[i]) {
        cnt++;
        ans = i;
      }
    win = (cnt == 1);
    if (win)
      printf("%d\n", ans);
    else
      cout << (crap ? "-1" : "0") << endl;
  }
  return 0;
}
