#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
pair<int, int> A[121], B[121];
int c;
int flag[121];
int calc(pair<int, int> a, pair<int, int> b) {
  int cnt = 0;
  if (a.first == b.first) cnt++;
  if (a.second == b.second) cnt++;
  if (a.first == b.second) cnt++;
  if (a.second == b.first) cnt++;
  if (cnt != 1) return 0;
  if (a.first == b.first) return a.first;
  if (a.first == b.second) return a.first;
  return a.second;
}
int main() {
  int n, m;
  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);
  vector<int> vec;
  for (int i = 1; i <= n; i++) {
    vector<int> res;
    for (int j = 1; j <= m; j++) {
      int cnt = 0;
      int first = calc(A[i], B[j]);
      if (first == 0) continue;
      res.push_back(first);
    }
    sort(res.begin(), res.end());
    res.resize(unique(res.begin(), res.end()) - res.begin());
    if (res.size() <= 0) continue;
    if (res.size() >= 2) return puts("-1"), 0;
    vec.push_back(res[0]);
  }
  for (int i = 1; i <= m; i++) {
    vector<int> res;
    for (int j = 1; j <= n; j++) {
      int cnt = 0;
      int first = calc(B[i], A[j]);
      if (first == 0) continue;
      res.push_back(first);
    }
    sort(res.begin(), res.end());
    res.resize(unique(res.begin(), res.end()) - res.begin());
    if (res.size() == 0) continue;
    if (res.size() >= 2) return puts("-1"), 0;
    vec.push_back(res[0]);
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  if (vec.size() >= 2) return puts("0"), 0;
  cout << vec[0] << endl;
}
