#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007;
const int inf = (1LL << 29);
pair<int, int> a[maxn], b[maxn];
set<int> s[2][maxn];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
  for (int i = 1; i <= m; i++) cin >> b[i].first >> b[i].second;
  int now = 0, ok = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= m; k++) {
      int e = 0, x = 0;
      if (a[i].first == b[k].first) e++, x = a[i].first;
      if (a[i].second == b[k].first) e++, x = a[i].second;
      if (a[i].first == b[k].second) e++, x = a[i].first;
      if (a[i].second == b[k].second) e++, x = a[i].second;
      if (e == 1) {
        s[0][i].insert(x);
        s[1][k].insert(x);
        if (now) {
          if (now != x) ok = 0;
        } else
          now = x;
      }
    }
  }
  if (ok == 0) {
    int o = 1;
    for (int i = 1; i <= n; i++) {
      if (s[0][i].size() > 1) o = 0;
    }
    for (int i = 1; i <= m; i++)
      if (s[1][i].size() > 1) o = 0;
    if (o)
      printf("0");
    else
      printf("-1");
  } else
    printf("%d", now);
  return 0;
}
