#include <bits/stdc++.h>
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...);
}
const int M = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<array<int, 2>> A(n), B(m);
  for (int i = 0; i < n; ++i) {
    cin >> A[i][0] >> A[i][1];
    sort(A[i].begin(), A[i].end());
  }
  for (int i = 0; i < m; ++i) {
    cin >> B[i][0] >> B[i][1];
    sort(B[i].begin(), B[i].end());
  }
  int cnt = 0;
  int t = 0;
  int c = 1;
  set<int> s;
  for (auto a : A) {
    for (auto b : B) {
      if (a == b) {
        continue;
      }
      if (a[0] == b[0] || a[0] == b[1]) {
        c = a[0];
        s.insert(c);
        ++t;
      } else if (a[1] == b[0] || a[1] == b[1]) {
        c = a[1];
        s.insert(c);
        ++t;
      }
    }
  }
  if ((int)s.size() == 1) {
    cout << c << "\n";
    return 0;
  }
  for (auto a : A) {
    set<int> s;
    for (auto b : B) {
      if (a == b) {
        continue;
      }
      if (a[0] == b[0] || a[0] == b[1]) {
        s.insert(a[0]);
      } else if (a[1] == b[0] || a[1] == b[1]) {
        s.insert(a[1]);
      }
    }
    if ((int)s.size() == 2) {
      cout << -1 << "\n";
      return 0;
    }
  }
  for (auto a : B) {
    set<int> s;
    for (auto b : A) {
      if (a == b) {
        continue;
      }
      if (a[0] == b[0] || a[0] == b[1]) {
        s.insert(a[0]);
      } else if (a[1] == b[0] || a[1] == b[1]) {
        s.insert(a[1]);
      }
    }
    if ((int)s.size() == 2) {
      cout << -1 << "\n";
      return 0;
    }
  }
  cout << 0 << "\n";
  return 0;
}
