#include <bits/stdc++.h>
using namespace std;
int check(int l, int r, pair<int, int> p) {
  if (l == p.first && r != p.second) return l;
  if (l == p.second && r != p.first) return l;
  if (r == p.first && l != p.second) return r;
  if (r == p.second && l != p.first) return r;
  return 0;
}
int vis[100];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<pair<int, int>> ps;
  for (int i = 0; i < (n); i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    ps.push_back({min(a, b), max(a, b)});
  }
  int total_cnt = 0;
  int ans = 0;
  for (int i = 0; i < (m); i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    int l = min(a, b);
    int r = max(a, b);
    int cnt = 0;
    int local = 0;
    for (int j = 0; j < (n); j++) {
      int curr = check(l, r, ps[j]);
      if (curr && vis[j] && vis[j] != curr) {
        puts("-1");
        do {
        } while (0);
        return 0;
      }
      if (curr) {
        if (local != curr) cnt++;
        do {
        } while (0);
        local = curr;
        vis[j] = curr;
      }
    }
    if (cnt == 1 && ans != local) {
      total_cnt++;
      ans = local;
    } else if (cnt > 1) {
      puts("-1");
      return 0;
    }
  }
  if (total_cnt == 1) {
    printf("%d\n", ans);
  } else
    puts("0");
}
