#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;
pair<int, int> a[maxn];
pair<int, int> b[maxn];
int da[maxn];
int db[maxn];
int va[maxn];
int vb[maxn];
int n, m;
int Chk(pair<int, int> x, pair<int, int> y) {
  if (x.first == y.first && x.second != y.second) return x.first;
  if (x.first == y.second && x.second != y.first) return x.first;
  if (x.second == y.first && x.first != y.second) return x.second;
  if (x.second == y.second && x.first != y.first) return x.second;
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].first, &b[i].second);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x = Chk(a[i], b[j]);
      if (!x) continue;
      if (x != va[i]) da[i]++, va[i] = x;
      if (x != vb[j]) db[j]++, vb[j] = x;
    }
  while (1) {
    bool sol = 1;
    int ansa = 0;
    for (int i = 1; i <= n; i++)
      if (da[i] >= 2 || (ansa && da[i] && va[i] != ansa))
        sol = 0;
      else if (da[i])
        ansa = va[i];
    for (int i = 1; i <= m; i++)
      if (db[i] >= 2 || (ansa && db[i] && vb[i] != ansa)) sol = 0;
    if (sol) {
      printf("%d\n", ansa);
      return 0;
    } else
      break;
  }
  for (int i = 1; i <= n; i++)
    if (da[i] >= 2) {
      printf("-1\n");
      return 0;
    }
  for (int i = 1; i <= m; i++)
    if (db[i] >= 2) {
      printf("-1\n");
      return 0;
    }
  printf("0\n");
  return 0;
}
