#include <bits/stdc++.h>
using namespace std;
const int N = 15;
pair<int, int> p[N], p2[N];
vector<int> adj[N], adj2[N];
int color[N], color2[N];
int test(int i, int j) {
  if (p[i].first == p2[j].first && p[i].second == p2[j].second) return false;
  if (p[i].first == p2[j].first) return true;
  if (p[i].first == p2[j].second) return true;
  if (p[i].second == p2[j].first) return true;
  if (p[i].second == p2[j].second) return true;
  return false;
}
int get(int i, int j) {
  if (p[i].first == p2[j].first) return p[i].first;
  if (p[i].first == p2[j].second) return p[i].first;
  if (p[i].second == p2[j].first) return p[i].second;
  if (p[i].second == p2[j].second) return p[i].second;
  return 0;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  memset(color, -1, sizeof color);
  memset(color2, -1, sizeof color2);
  for (int i = 0; i < n; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (x > y) swap(x, y);
    p[i] = make_pair(x, y);
  }
  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (x > y) swap(x, y);
    p2[i] = make_pair(x, y);
  }
  int val = -1, ok = 1, ok2 = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (test(i, j)) {
        adj[i].push_back(j);
        adj2[j].push_back(i);
        if (val == -1) {
          val = get(i, j);
          if (color[i] == -1) {
            color[i] = val;
          } else if (color[i] != val) {
            ok2 = 0;
          }
          if (color2[j] == -1) {
            color2[j] = val;
          } else if (color2[j] != val) {
            ok2 = 0;
          }
        } else if (val != get(i, j)) {
          ok = 0;
          val = get(i, j);
          if (color[i] == -1) {
            color[i] = val;
          } else if (color[i] != val) {
            ok2 = 0;
          }
          if (color2[j] == -1) {
            color2[j] = val;
          } else if (color2[j] != val) {
            ok2 = 0;
          }
        } else {
          if (color[i] == -1) {
            color[i] = val;
          } else if (color[i] != val) {
            ok2 = 0;
          }
          if (color2[j] == -1) {
            color2[j] = val;
          } else if (color2[j] != val) {
            ok2 = 0;
          }
        }
      }
    }
  }
  int mx = 0;
  for (int i = 0; i < n; ++i) {
    if (adj[i].size() > mx) {
      mx = adj[i].size();
    }
  }
  for (int i = 0; i < m; ++i) {
    if (adj2[i].size() > mx) {
      mx = adj[i].size();
    }
  }
  if (ok == 1) {
    printf("%d\n", val);
  } else if (ok2 == 1) {
    puts("0");
  } else {
    puts("-1");
  }
}
