#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
set<long long> handle(pair<long long, long long> *a,
                      pair<long long, long long> *b, long long n, long long m) {
  set<long long> poss;
  for (long long i = 0; i < n; ++i) {
    long long cnt = 0;
    set<long long> cur;
    for (long long j = 0; j < m; ++j) {
      if ((a[i].first == b[j].first && a[i].second == b[j].second) ||
          (a[i].first == b[j].second && a[i].second == b[j].first))
        continue;
      if (a[i].first == b[j].first)
        ++cnt, poss.insert(a[i].first), cur.insert(a[i].first);
      else if (a[i].first == b[j].second)
        ++cnt, poss.insert(a[i].first), cur.insert(a[i].first);
      else if (a[i].second == b[j].first)
        ++cnt, poss.insert(a[i].second), cur.insert(a[i].second);
      else if (a[i].second == b[j].second)
        ++cnt, poss.insert(a[i].second), cur.insert(a[i].second);
    }
    if (cur.size() > 1) {
      cout << -1 << '\n';
      exit(0);
    }
  }
  return poss;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  pair<long long, long long> a[n], b[m];
  for (long long i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
  for (long long i = 0; i < m; ++i) cin >> b[i].first >> b[i].second;
  set<long long> poss = handle(a, b, n, m);
  handle(b, a, m, n);
  long long ans = 0;
  if (poss.size() == 1) ans = *poss.begin();
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t = 1;
  while (t--) solve();
}
