#include <bits/stdc++.h>
using namespace std;
int common(vector<int>& a, vector<int>& b) {
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < b.size(); j++)
      if (a[i] == b[j]) return a[i];
  return -1;
}
int cntcommon(vector<int>& a, vector<int>& b) {
  int ans = 0;
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < b.size(); j++)
      if (a[i] == b[j]) ans++;
  return ans;
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(2)), b(m, vector<int>(2));
  for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1];
  for (int i = 0; i < m; i++) cin >> b[i][0] >> b[i][1];
  vector<int> ipos(10);
  vector<vector<int>> apos(n, vector<int>(10)), bpos(m, vector<int>(10));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int c = cntcommon(a[i], b[j]);
      if (c != 1) continue;
      c = common(a[i], b[j]);
      ipos[c]++;
      apos[i][c]++;
      bpos[j][c]++;
    }
  }
  bool iknow = 1, aknows = 1, bknows = 1;
  int num = -1;
  for (int i = 1; i < 10; i++)
    if (ipos[i] > 0)
      if (num == -1)
        num = i;
      else
        iknow = 0;
  for (int i = 0; i < n; i++) {
    bool seen = 0;
    for (int j = 1; j < 10; j++)
      if (apos[i][j] > 0)
        if (!seen)
          seen = 1;
        else
          aknows = 0;
  }
  for (int i = 0; i < m; i++) {
    bool seen = 0;
    for (int j = 1; j < 10; j++)
      if (bpos[i][j] > 0)
        if (!seen)
          seen = 1;
        else
          bknows = 0;
  }
  if (iknow)
    cout << num << "\n";
  else if (aknows && bknows)
    cout << "0\n";
  else
    cout << "-1\n";
}
