#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1);
const long long INFF = 1e18;
const int INF = 1e9;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
pair<int, int> a[MAX], b[MAX];
int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  ;
  for (int i = (1); i < (n + 1); ++i) {
    scanf("%d %d", &a[i].first, &a[i].second);
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = (1); i < (m + 1); ++i) {
    scanf("%d %d", &b[i].first, &b[i].second);
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  int ans = 1;
  set<int> st1;
  for (int i = (1); i < (n + 1); ++i) {
    bool f1 = 0, f2 = 0;
    for (int j = (1); j < (m + 1); ++j) {
      if (a[i].first == b[j].first && a[i].second == b[j].second) continue;
      if (a[i].first == b[j].first)
        f1 = 1;
      else if (a[i].first == b[j].second)
        f1 = 1;
      else if (a[i].second == b[j].first)
        f2 = 1;
      else if (a[i].second == b[j].second)
        f2 = 1;
    }
    if (f1 && f2) {
      puts("-1");
      return 0;
    } else if (f1)
      st1.insert(a[i].first);
    else if (f2)
      st1.insert(a[i].second);
  }
  set<int> st2;
  for (int i = (1); i < (m + 1); ++i) {
    bool f1 = 0, f2 = 0;
    for (int j = (1); j < (n + 1); ++j) {
      if (b[i].first == a[j].first && b[i].second == a[j].second) continue;
      if (b[i].first == a[j].first)
        f1 = 1;
      else if (b[i].first == a[j].second)
        f1 = 1;
      else if (b[i].second == a[j].first)
        f2 = 1;
      else if (b[i].second == a[j].second)
        f2 = 1;
    }
    if (f1 && f2) {
      puts("-1");
      return 0;
    } else if (f1)
      st2.insert(b[i].first);
    else if (f2)
      st2.insert(b[i].second);
  }
  if (((int)st1.size()) >= 2 && ((int)st2.size()) >= 2)
    ans = 0;
  else
    ans = *st1.begin();
  printf("%d\n", ans);
  return 0;
}
