#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[12], b[12];
pair<int, int> ree(pair<int, int> a) {
  return {min(a.first, a.second), max(a.first, a.second)};
}
bool mtch[10];
bool fa[13], sa[13];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d %d", &a[i].first, &a[i].second), a[i] = ree(a[i]);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &b[i].first, &b[i].second), b[i] = ree(b[i]);
    bool ff = false, ss = false;
    for (int j = 0; j < n; j++) {
      if (b[i] == a[j]) continue;
      int nm = -1;
      if (a[j].first == b[i].first) ff = true, fa[j] = true, nm = a[j].first;
      if (a[j].first == b[i].second) ss = true, fa[j] = true, nm = a[j].first;
      if (a[j].second == b[i].second) ss = true, sa[j] = true, nm = a[j].second;
      if (a[j].second == b[i].first) ff = true, sa[j] = true, nm = a[j].second;
      if (nm == -1) continue;
      mtch[nm] = true;
    }
    if (ff && ss) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < n; i++)
    if (fa[i] && sa[i]) {
      printf("-1\n");
      return 0;
    }
  int cnt = 0, nm;
  for (int i = 1; i < 10; i++)
    if (mtch[i]) cnt++, nm = i;
  if (cnt == 1) {
    printf("%d\n", nm);
    return 0;
  }
  printf("0\n");
}
