#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
pair<int, int> a[maxn], b[maxn];
bool check(pair<int, int> &x, pair<int, int> &y) {
  return (x.first == y.first && x.second == y.second) ||
         (x.second == y.first && x.first == y.second);
}
int ok(pair<int, int> &x, pair<int, int> &y) {
  if (x.first == y.first) return x.first;
  if (x.second == y.second) return x.second;
  if (x.first == y.second) return x.first;
  if (x.second == y.first) return x.second;
  return 0;
}
int vis[maxn];
int main() {
  int n, m, f, t;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].first, &b[i].second);
  for (int i = 1; i <= n; i++) {
    f = 0;
    for (int j = 1; j <= m; j++) {
      if (check(a[i], b[j])) continue;
      t = ok(a[i], b[j]);
      if (t == 0) continue;
      if (f && f != t)
        f = -1;
      else
        f = t;
    }
    if (f == -1) return puts("-1"), 0;
    if (f) vis[f] = 1;
  }
  for (int i = 1; i <= m; i++) {
    f = 0;
    for (int j = 1; j <= n; j++) {
      if (check(b[i], a[j])) continue;
      t = ok(b[i], a[j]);
      if (t == 0) continue;
      if (f && f != t)
        f = -1;
      else
        f = t;
    }
    if (f == -1) return puts("-1"), 0;
    if (f) vis[f] = 1;
  }
  int res = -1;
  for (int i = 1; i <= 9; i++)
    if (vis[i]) {
      if (res != -1)
        res = 0;
      else
        res = i;
    }
  printf("%d\n", res);
  return 0;
}
