#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m;
  cin >> n >> m;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    a[i] = make_pair(x, y);
  }
  vector<pair<int, int>> b(m);
  vector<pair<int, int>> ua(n, {0, 0});
  vector<pair<int, int>> ub(m, {0, 0});
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    b[i] = make_pair(x, y);
    for (int j = 0; j < n; ++j) {
      if (x == a[j].first && y != a[j].second) {
        ++ub[i].first;
        ++ua[j].first;
      }
      if (y == a[j].first) {
        ++ua[j].first;
        ++ub[i].second;
      }
      if (x != a[j].first && y == a[j].second) {
        ++ua[j].second;
        ++ub[i].second;
      }
      if (x == a[j].second) {
        ++ub[i].first;
        ++ua[j].second;
      }
    }
  }
  vector<bool> z(10, false);
  for (int i = 0; i < n; ++i) {
    if (ua[i].first > 0 && ua[i].second > 0) {
      cout << -1;
      return 0;
    }
    if (ua[i].first > 0) {
      z[a[i].first] = 1;
    }
    if (ua[i].second > 0) {
      z[a[i].second] = 1;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (ub[i].first > 0 && ub[i].second > 0) {
      cout << -1;
      return 0;
    }
  }
  int k = 0;
  int ans = 0;
  for (int i = 1; i <= 9; ++i) {
    if (z[i]) {
      ++k;
      ans = i;
    }
  }
  cout << (k > 1 ? 0 : ans);
  return 0;
}
