#include <bits/stdc++.h>
using namespace std;
int same(pair<long long, long long> A, pair<long long, long long> B) {
  if (A.first == B.first && A.second == B.second) {
    return 2;
  }
  if (A.first == B.first) {
    return 1;
  }
  if (A.second == B.second) {
    return 1;
  }
  if (A.first == B.second) {
    return 1;
  }
  if (A.second == B.first) {
    return 1;
  }
  return 0;
}
long long numSame(pair<long long, long long> A, pair<long long, long long> B) {
  cerr << "<=========================>" << endl;
  cerr << A.first << " " << A.second << endl;
  cerr << B.first << " " << B.second << endl;
  cerr << ">=========================<" << endl;
  if (A.first == B.first) {
    return A.first;
  }
  if (A.second == B.second) {
    return A.second;
  }
  if (A.first == B.second) {
    return A.first;
  }
  if (A.second == B.first) {
    return A.second;
  }
  return -1;
}
int main() {
  long long N, M;
  scanf("%lld", &N);
  scanf("%lld", &M);
  pair<long long, long long> dat[N];
  pair<long long, long long> d[M];
  for (long long i = 0; i < N; ++i) {
    scanf("%lld", &dat[i].first);
    scanf("%lld", &dat[i].second);
    if (dat[i].first > dat[i].second) {
      swap(dat[i].first, dat[i].second);
    }
  }
  for (long long i = 0; i < M; ++i) {
    scanf("%lld", &d[i].first);
    scanf("%lld", &d[i].second);
    if (d[i].first > d[i].second) {
      swap(d[i].first, d[i].second);
    }
  }
  set<long long> ans;
  for (long long i = 0; i < N; ++i) {
    set<long long> curr;
    for (long long j = 0; j < M; ++j) {
      if (same(dat[i], d[j]) == 1) {
        curr.insert(numSame(dat[i], d[j]));
      } else {
      }
    }
    if (curr.size() > 1) {
      puts("-1");
      return 0;
    } else {
      for (long long x : curr) {
        ans.insert(x);
      }
    }
  }
  if (ans.size() == 1) {
    cout << (*ans.begin()) << endl;
  } else {
    ans.clear();
    for (long long i = 0; i < M; ++i) {
      set<long long> curr;
      for (long long j = 0; j < N; ++j) {
        if (same(d[i], dat[j]) == 1) {
          curr.insert(numSame(d[i], dat[j]));
        } else {
        }
      }
      if (curr.size() > 1) {
        for (long long x : curr) {
          cerr << "-> " << x << endl;
        }
        cerr << "asu" << endl;
        puts("-1");
        return 0;
      } else {
        for (long long x : curr) {
          ans.insert(x);
        }
      }
    }
    if (ans.size() == 1) {
      cout << (*ans.begin()) << endl;
    } else {
      puts("0");
    }
  }
  return 0;
}
