#include <bits/stdc++.h>
using namespace std;
int canI(vector<int> &a, vector<int> &b) {
  int cand = 0;
  for (auto x : a) {
    for (auto y : b) {
      auto xy = x & y;
      if (__builtin_popcount(xy) == 1) cand |= xy;
    }
  }
  if (__builtin_popcount(cand) > 1) return 0;
  return __builtin_ctz(cand);
}
bool can(vector<int> &a, vector<int> &b) {
  for (auto x : a) {
    int cand = 0;
    for (auto y : b) {
      auto xy = x & y;
      if (__builtin_popcount(xy) == 1) cand |= xy;
    }
    if (__builtin_popcount(cand) > 1) return 0;
  }
  return 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] |= (1 << x) | (1 << y);
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    b[i] |= (1 << x) | (1 << y);
  }
  int ans = canI(a, b);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << (can(a, b) && can(b, a) ? 0 : -1) << endl;
  return 0;
}
