class Table {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  success() {
    print(0);
  }
  fail() {
    print(-1);
  }
  init() {
    let reg = /[1-9]/;
    return reg.test(this.x) && reg.test(this.y) ? this.success() : this.fail();
  }
  check(coord, type) {
    return coord <= this[type] - 1;
  }
  exchange(arr) {
    arr.every((coord, index) => this.check(coord, index % 2 === 0 ? "x" : "y"))
      ? this.success()
      : this.fail();
  }
  addRow(x) {
    this.check(x, "x") && this.x !== 9 ? this.success() : this.fail();
  }
  addCol(y) {
    this.check(y, "y") && this.y !== 9 ? this.success() : this.fail();
  }
  search(arr) {
    arr.every((coord, index) => this.check(coord, index % 2 === 0 ? "x" : "y"))
      ? this.success()
      : this.fail();
  }
}

let str;
while ((str = readline())) {
  const table = new Table(...str.split(" ").map(Number));
  table.init();
  table.exchange(readline().split(" ").map(Number));
  table.addRow(parseInt(readline()));
  table.addCol(parseInt(readline()));
  table.search(readline().split(" ").map(Number));
}