#include <bits/stdc++.h>
using namespace std;
long long t, n, x, y, z, d, a[1000005], b[1000005], c[1000005],
    minn = LONG_LONG_MAX, maxx = -LONG_MAX, a2[1000005], b2[1000050], pos,
    ans[15], m;
map<long long, long long> m1;
map<long long, long long> m2;
string s, s1, s2;
char cc;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> a2[i] >> b2[i];
  }
  d = 0;
  for (int i = 0; i < n; i++) {
    pos = -1;
    for (int j = 0; j < m; j++) {
      c[a[i]]++;
      c[b[i]]++;
      c[a2[j]]++;
      c[b2[j]]++;
      pos = -1;
      for (int k = 1; k <= 9; k++) {
        if (c[k] == 2) {
          if (pos == -1) {
            pos = k;
          } else {
            pos = -1;
          }
        }
      }
      if (pos != -1) {
        ans[pos] = 1;
      }
      c[a[i]]--;
      c[b[i]]--;
      c[a2[j]]--;
      c[b2[j]]--;
    }
  }
  pos = -1;
  for (int i = 1; i <= 9; i++) {
    if (ans[i]) {
      if (pos == -1) {
        pos = i;
      } else {
        pos = 10;
      }
    }
  }
  for (int i = 0; i < 10; i++) {
    ans[i] = 0;
  }
  if (pos >= 1 && pos <= 9) {
    cout << pos;
  } else {
    d = 0;
    for (int i = 0; i < n; i++) {
      pos = -1;
      for (int j = 0; j < m; j++) {
        c[a[i]]++;
        c[b[i]]++;
        c[a2[j]]++;
        c[b2[j]]++;
        pos = -1;
        for (int k = 1; k <= 9; k++) {
          if (c[k] == 2) {
            if (pos == -1) {
              pos = k;
            } else {
              pos = -1;
            }
          }
        }
        if (pos != -1) {
          ans[pos] = 1;
        }
        c[a[i]]--;
        c[b[i]]--;
        c[a2[j]]--;
        c[b2[j]]--;
        pos = -1;
      }
      pos = -1;
      for (int kk = 1; kk <= 9; kk++) {
        if (ans[kk]) {
          if (pos == -1) {
            pos = i;
          } else {
            pos = 10;
          }
        }
      }
      for (int kk = 0; kk < 10; kk++) {
        ans[kk] = 0;
      }
      if (pos > 9) {
        d = 1;
      }
    }
    for (int i = 0; i < 10; i++) {
      ans[i] = 0;
    }
    for (int i = 0; i < m; i++) {
      pos = -1;
      for (int j = 0; j < n; j++) {
        c[a[j]]++;
        c[b[j]]++;
        c[a2[i]]++;
        c[b2[i]]++;
        pos = -1;
        for (int k = 1; k <= 9; k++) {
          if (c[k] == 2) {
            if (pos == -1) {
              pos = k;
            } else {
              pos = -1;
            }
          }
        }
        if (pos != -1) {
          ans[pos] = 1;
        }
        c[a[j]]--;
        c[b[j]]--;
        c[a2[i]]--;
        c[b2[i]]--;
        pos = -1;
      }
      pos = -1;
      for (int kk = 1; kk <= 9; kk++) {
        if (ans[kk]) {
          if (pos == -1) {
            pos = i;
          } else {
            pos = 10;
          }
        }
      }
      for (int kk = 0; kk < 10; kk++) {
        ans[kk] = 0;
      }
      if (pos > 9) {
        d = 1;
      }
    }
    for (int i = 0; i < 10; i++) {
      ans[i] = 0;
    }
    if (d) {
      cout << -1;
    } else {
      cout << 0;
    }
  }
}
