#include <bits/stdc++.h>
using namespace std;
int solve(vector<pair<int, int>> a, vector<pair<int, int>> b) {
  int v = -1;
  int c = 1;
  bool va = 1;
  for (int i = 0; i < a.size(); ++i) {
    int match = -1;
    bool valid = 1;
    for (int j = 0; j < b.size(); ++j) {
      if (a[i].first == b[j].first && a[i].second == b[j].second) {
        continue;
      }
      if (a[i].first == b[j].first || a[i].first == b[j].second) {
        if (match == -1 || match == a[i].first)
          match = a[i].first;
        else
          valid = 0;
      }
      if (a[i].second == b[j].first || a[i].second == b[j].second) {
        if (match == -1 || match == a[i].second)
          match = a[i].second;
        else
          valid = 0;
      }
    }
    if (!valid) va = 0;
    if (match != -1) {
      if (v == -1) {
        v = match;
      } else if (v != match) {
        c++;
      }
    }
  }
  if (!va) {
    return -1;
  }
  if (c > 1) return 0;
  return v;
}
int main() {
  {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  }
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a(n), b(m);
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    a[i].first = min(u, v);
    a[i].second = max(u, v);
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    b[i].first = min(u, v);
    b[i].second = max(u, v);
  }
  int a1 = solve(a, b);
  int a2 = solve(b, a);
  if (a1 == -1 || a2 == -1) {
    cout << -1 << endl;
  } else if ((a1 == 0 || a2 == 0) || (a1 != a2)) {
    cout << 0 << endl;
  } else
    cout << a2 << endl;
  return 0;
}
