CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
A = [-1]
for _ in range(n):
    A.append(int(eval(input())))

X = [0]*(n+1)
count = 0
if A[1] != 0:
    print((-1))
else:
    for i in range(n,0,-1):
        if A[i] > X[i]:
            for j in range(A[i]):
                X[i-j] = A[i] - j
            count += A[i]
        elif A[i] < X[i]:
            print((-1))
            break
    else:
        print(count)
    

# Optimized solution
n = int(eval(input()))
A = [-1]
for _ in range(n):
    A.append(int(eval(input())))

num = 0
count = 0
if A[1] != 0:
    print((-1))
else:
    for i in range(n,0,-1):
        if A[i] > num:
            num = A[i]
            count += num
        elif A[i] < num:
            print((-1))
            break
        num -= 1
    else:
        print(count)
# Original solution
n=int(eval(input()))

a=[int(eval(input())) for _ in range(n)]
#a(n+1)!=0の時、a(n+1)=a(n)+1である

cnt=0

if a[0]!=0:
    print((-1))
    exit()

b=[0]*n
#print(a)
for ii in range(1,n):
    i=n-ii
    if a[i]==b[i]:
        continue
    elif a[i]<b[i] or a[i]>i:
        print((-1))
        exit()
    else:
        for k in range(a[i]+1):
            b[i-k]=a[i]-k
        cnt+=a[i]

    #print(b)

print(cnt)

    
"""
t: 0 1 1 0 1 2 2 1 2
s: 0 0 0 0 0 0 0 0 0
1: 0 0 1 0 0 0 0 0 0
2: 0 1 1 0 0 0 0 0 0
3: 0 1 1 0 0 0 0 1 0
4: 0 1 1 0 0 0 0 1 2
5: 0 1 1 0 0 1 0 1 2

"""





# Optimized solution
n=int(eval(input()))

a=[int(eval(input())) for _ in range(n)]
#a(n+1)!=0の時、a(n+1)=a(n)+1である

cnt=0

if a[0]!=0:
    print((-1))
    exit()
#print(" ".join(list(map(str,a))))
for ii in range(1,n):
    i=n-ii
    if a[i]==0:
        continue
    elif a[i]<=a[i-1]:
        cnt+=a[i]
    elif a[i]==a[i-1]+1:
        cnt+=1
    elif a[i]>a[i-1]+1 or  a[i]>i:
        print((-1))
        exit()
    #print(b)
    #print(" "*(i+1),"^",cnt)

print(cnt)

    
"""
t: 0 1 1 0 1 2 2 1 2
s: 0 0 0 0 0 0 0 0 0
1: 0 0 1 0 0 0 0 0 0
2: 0 1 1 0 0 0 0 0 0
3: 0 1 1 0 0 0 0 1 0
4: 0 1 1 0 0 0 0 1 2
5: 0 1 1 0 0 1 0 1 2

"""
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)][::-1]

if A[-1] != 0:
    print((-1))
else:
    bef = A[0]
    c, ans = 0, 0
    for i in A:
        if bef!=i:
            if bef-i > 1:
                print((-1))
                break
            ans += bef*c
            c = 1 if bef<i else 0
        else:
            c += 1
        bef = i
    else:
        print(ans)

# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)][::-1]

if A[-1]:
    print((-1))
else:
    bef = A[0]
    c, ans = 0, 0
    for i in A:
        if bef!=i:
            if bef-i > 1:
                ans = -1
                break
            ans += bef*c
            c = 1 if bef<i else 0
        else:
            c += 1
        bef = i
    print(ans)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
ans = 0
if a[0] != 0:
    print((-1))
    exit()
for i in range(n-1):
    if a[i+1] - a[i] > 1:
        print((-1))
        exit()
for _ in range(n):
    if set(a) == set([-1,0]) or set(a) == set([0]):
        break
    i = a.index(max(a))
    if i == 0:
        print((-1))
        exit()
    if a[i-1] + 1 == a[i]:
        a[i] = -1
        ans += 1
    elif a[i-1] == -1:
        a[i-1] = a[i] - 1
        a[i] = -1
        ans += 1
    else:
        print((-1))
        exit()
print(ans)

# Optimized solution
n = int(eval(input()))
prev = ans = -1
a = [int(eval(input())) for _ in range(n)]
for i in a:
    if i - prev > 1:
        print((-1))
        exit()
    elif i - prev == 1:
        ans += 1
    else:
        ans += i
    prev = i
print(ans)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]
if A[0]:
    print((-1))
    exit()

ans = 0
for a,b in zip(A,A[1:]):
    if b-a > 1:
        print((-1))
        exit()
    if b==0: continue
    if b-a == 1:
        ans += 1
    else:
        ans += b
print(ans)

# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]

if A[0]:
    print((-1))
    exit()

ans = 0
for a,b in zip(A,A[1:]):
    if a+1 < b:
        print((-1))
        exit()
    if a+1 == b:
        ans += 1
    else:
        ans += b
print(ans)
# Original solution
n = int(eval(input()))
A = [int(eval(input()))for _ in range(n)]
if A[0] != 0:
    print((-1))
    exit()

ans = A[-1]
for a, prev_a in reversed(tuple(zip(A, A[1:]))):
    if a == prev_a-1:
        continue
    if a < prev_a-1:
        print((-1))
        break
    ans += a
else:
    print(ans)


# Optimized solution
n = int(eval(input()))
ans = -1
prev = -1
for _ in range(n):
    a = int(eval(input()))
    if prev+1 == a:
        prev = a
        ans += 1
        continue
    if prev+1 < a:
        print((-1))
        break
    ans += a
    prev = a
else:
    print(ans)
# Original solution
n = int(eval(input()))

a = []
for i in range(n):
    a.append(int(eval(input())))

if a[0] > 0:
    print((-1))
    exit()

for i in range(1, n):
    if a[i] > i:
        print((-1))
        exit()
    if a[i] > a[i-1] + 1:
        print((-1))
        exit()

result = 0
last = n
b = [0 for i in range(n)]
while 1:
    for i in range(last):
        j = last - i - 1
        if a[j] != b[j]:
            start = j
            last = j
            max_diff = a[j]
            break
    if j == 0:
        print(result)
        exit()
    for i in range(max_diff):
        b[start - i] = max_diff - i
    result += max_diff

# Optimized solution
n = int(eval(input()))

a = []
for i in range(n):
    a.append(int(eval(input())))

if a[0] > 0:
    print((-1))
    exit()

for i in range(1, n):
    if a[i] > i:
        print((-1))
        exit()
    if a[i] > a[i - 1] + 1:
        print((-1))
        exit()

if n == 1:
    print((0))
    exit()

result = 0

if a[n-1] != 0:
    result += a[n-1]

for i in range(2, n):
    j = n - i
    if a[j] != 0 and a[j] != a[j + 1] - 1:
            result += a[j]

print(result)
# Original solution
n, *a = list(map(int, open(0).read().split()))

# hantei
ok = True
for i in range(n - 1):
    if a[i + 1] - a[i] > 1:
        ok = False
if not ok or a[0] != 0:
    print((-1))
    exit()


ans = 0
a = list(reversed(a))
i = 0
while i < n:
    ans += a[i]
    while i < n - 1 and a[i] - 1 == a[i + 1]:
        i += 1
    i += 1
print(ans)


# Optimized solution
n, *a = list(map(int, open(0).read().split()))
# 状態1: iとi+1が1違う
# 状態2: iとi+1が同じ
# これ以外のiとi+1の関係があったら構築できない

# 状態1ではi+1を作る過程でiも作れる。
# 状態2ではi+1を作った後にiを作る必要がある。
# 後ろから見てa[i] - 1 == a[i - 1]となってる数を数えなければおｋ

# check
if a[0] != 0:
	print((-1))
	exit()
for i in range(n - 1):
	if a[i + 1] - a[i] > 1:
		print((-1))
		exit()

# count
i = n - 1
ans = 0
while i > 0:
	ans += a[i]
	while i > 0 and a[i] - 1 == a[i - 1]:
		i -= 1
	i -= 1
print(ans)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
if A[0]:
    print((-1))
    exit()
ans = 0
dp = A[-1]
for i in range(N - 1, 0, -1):
    if A[i - 1] == A[i] - 1:
        continue
    elif A[i - 1] >= A[i]:
        ans += dp
        dp = A[i - 1]
    else:
        print((-1))
        exit()
print((ans + dp))


# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
if A[0]:
    print((-1))
    exit()
ans = 0
dp = A[-1]
for a, b in zip(A[-2::-1], A[-1::-1]):
    if a == b - 1:
        continue
    elif a >= b:
        ans += dp
        dp = a
    else:
        print((-1))
        exit()
print((ans + dp))
# Original solution
N=int(eval(input()))
A=[int(eval(input())) for _ in range(N)]

ans=0
if A[0]!=0:
    print((-1))
    exit()
for i in range(N-1,0,-1):
    l,a=A[i-1],A[i]
    if a>l+1:
        print((-1))
        exit()
    elif a==l+1:
        ans+=1
    elif a!=0:
        ans+=a
print(ans)

# Optimized solution
import sys
def main():
    input = sys.stdin.readline
    N=int(eval(input()))
    A=[int(eval(input())) for _ in range(N)]

    if A[0]!=0:
        print((-1))
        exit()
    ans=0
    d=0
    for i in range(1,N):
        if A[i]==0: continue
        if A[i]>d+1:
            print((-1))
            exit()
        d=A[i]
        if A[i]==A[i-1]+1:
            ans+=1
        else:
            ans+=A[i]

    print(ans)

if __name__ == '__main__':
    main()
# Original solution
import sys
import math
import collections
import bisect
import copy
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===


def main():
    n = ni()
    a = [ni() for _ in range(n)]
    ans = 0

    if a[0] != 0:
        print((-1))
        exit(0)

    prev = -1
    for i, ai in enumerate(reversed(a)):
        if ai > n - i - 1:
            print((-1))
            exit(0)

        if i == 0:
            ans += ai
        else:
            if ai != prev - 1:
                if ai > prev - 1:
                    ans += ai
                else:
                    print((-1))
                    exit(0)
        prev = ai
    print(ans)

if __name__ == '__main__':
    main()


# Optimized solution
import sys
import math
import collections
import bisect
import copy
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===


def main():
    n = ni()
    a = [ni() for _ in range(n)]
    ans = 0

    prev = -1
    for i, ai in enumerate(reversed(a)):
        if ai > n - i - 1:
            print((-1))
            exit(0)

        if i == 0:
            ans += ai
        else:
            if ai != prev - 1:
                if ai > prev - 1:
                    ans += ai
                else:
                    print((-1))
                    exit(0)
        prev = ai
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
n=int(eval(input()))
A=[int(eval(input())) for _ in range(n)]

if A[0]!=0:print((-1));exit()

cnt=0
for i in range(1,n):
    if A[i-1]+1==A[i]:
        cnt +=1
    elif A[i-1]==A[i]:
        cnt +=A[i]
    elif A[i-1]>A[i]:
        cnt +=A[i]
    else:print((-1));exit()

print(cnt)

# Optimized solution
n=int(eval(input()))
A=[int(eval(input())) for _ in range(n)]

if A[0]!=0:print((-1));exit()

cnt=0
for i in range(1,n):
    if A[i-1]+1==A[i]:
        cnt +=1
    elif A[i-1]==A[i] or A[i-1]>A[i]:
        cnt +=A[i]
    else:print((-1));exit()

print(cnt)
# Original solution
N, W, H = list(map(int, input().split()))
row, col = [0] * (H + 1), [0] * (W + 1)
for _ in range(N):
    x, y, w = list(map(int, input().split()))
    row[max(0, y - w)] += 1
    row[min(H, y + w)] -= 1
    col[max(0, x - w)] += 1
    col[min(W, x + w)] -= 1
flag1 = flag2 = True
for i in range(H):
    row[i + 1] += row[i]
    flag1 &= row[i] > 0
if flag1:
    print("Yes")
    quit()
for i in range(W):
    col[i + 1] += col[i]
    flag2 &= col[i] > 0
if flag2:
    print("Yes")
else:
    print("No")

# Optimized solution
N, W, H = list(map(int, input().split()))
row, col = [0] * (H + 1), [0] * (W + 1)
for _ in range(N):
    x, y, w = list(map(int, input().split()))
    row[max(0, y - w)] += 1
    row[min(H, y + w)] -= 1
    col[max(0, x - w)] += 1
    col[min(W, x + w)] -= 1
for i in range(H):
    row[i + 1] += row[i]
    if not row[i]:
        break
else:
    print("Yes")
    quit()
for i in range(W):
    col[i + 1] += col[i]
    if not col[i]:
        break
else:
    print("Yes")
    quit()
print("No")
# Original solution
N, W, H = [int(x) for x in input().split()]

map_x = [False] * W
count_x = 0
map_y = [False] * H
count_y = 0

wifi_x = []
wifi_y = []
for _ in range(N):
    x, y, w = [int(x) for x in input().split()]
    wifi_x.append((x - w, x + w))
    wifi_y.append((y - w, y + w))
wifi_x.sort(key=lambda a: a[1], reverse=True)
wifi_y.sort(key=lambda a: a[1], reverse=True)


def check_wifi(wifi, l):
    for x1, x2 in wifi:
        if x1 <= l < x2:
            return x2
    return None


ans_x = True
b = 0
for x in range(W):
    if x < b:
        continue

    b = check_wifi(wifi_x, x)
    if not b:
        ans_x = False
        break

if not ans_x:
    b = 0
    for y in range(H):
        if y < b:
            continue

        b = check_wifi(wifi_y, y)
        if not b:
            print('No')
            exit()

print('Yes')

# Optimized solution
N, W, H = [int(x) for x in input().split()]

map_x = [0] * W
map_y = [0] * H

for _ in range(N):
    x, y, w = [int(x) for x in input().split()]
    map_x[max(0, x - w)] = max(map_x[max(0, x - w)], x + w - max(0, x - w))
    map_y[max(0, y - w)] = max(map_y[max(0, y - w)], y + w - max(0, y - w))


def check_wifi(wifi):
    m = 0
    end = len(wifi)

    for i, x in enumerate(wifi):
        if x:
            m = max(m, i + x)

        if m >= end:
            return True

        if i < m:
            continue

        return False

    return False

print(('Yes' if check_wifi(map_x) or check_wifi(map_y) else 'No'))
# Original solution
N, W, H = [int(x) for x in input().split()]

map_x = [0] * W
map_y = [0] * H

for _ in range(N):
    x, y, w = [int(x) for x in input().split()]
    map_x[max(0, x - w)] = max(map_x[max(0, x - w)], x + w - max(0, x - w))
    map_y[max(0, y - w)] = max(map_y[max(0, y - w)], y + w - max(0, y - w))


def check_wifi(wifi):
    m = 0
    end = len(wifi)

    for i, x in enumerate(wifi):
        if x:
            m = max(m, i + x)

        if m >= end:
            return True

        if i < m:
            continue

        return False

    return False

print(('Yes' if check_wifi(map_x) or check_wifi(map_y) else 'No'))

# Optimized solution
N, W, H = [int(x) for x in input().split()]

map_x = [0] * W
map_y = [0] * H

for _ in range(N):
    x, y, w = [int(x) for x in input().split()]
    map_x[max(0, x - w)] = max(map_x[max(0, x - w)], x + w - max(0, x - w))
    map_y[max(0, y - w)] = max(map_y[max(0, y - w)], y + w - max(0, y - w))


def check_wifi(wifi):
    m = 0
    end = len(wifi)

    for i, x in enumerate(wifi):
        if x and i + x > m:
            m = i + x

        if m >= end:
            return True

        if i < m:
            continue

        return False

    return False


print(('Yes' if check_wifi(map_x) or check_wifi(map_y) else 'No'))
# Original solution
N, W, H = [int(x) for x in input().split()]

map_x = [0] * W
map_y = [0] * H

for _ in range(N):
    x, y, w = [int(x) for x in input().split()]
    map_x[max(0, x - w)] = max(map_x[max(0, x - w)], x + w - max(0, x - w))
    map_y[max(0, y - w)] = max(map_y[max(0, y - w)], y + w - max(0, y - w))


def check_wifi(wifi):
    m = 0
    end = len(wifi)

    for i, x in enumerate(wifi):
        if x and i + x > m:
            m = i + x

        if m >= end:
            return True

        if i < m:
            continue

        return False

    return False


print(('Yes' if check_wifi(map_x) or check_wifi(map_y) else 'No'))

# Optimized solution
N, W, H = [int(x) for x in input().split()]

map_x = [0] * W
map_y = [0] * H

for _ in range(N):
    x, y, w = [int(x) for x in input().split()]
    mx = max(0, x - w)
    map_x[mx] = max(map_x[mx], x + w - mx)
    my = max(0, y - w)
    map_y[my] = max(map_y[my], y + w - my)


def check_wifi(wifi):
    m = 0
    end = len(wifi)

    for i, x in enumerate(wifi):
        if x and i + x > m:
            m = i + x

        if m >= end:
            return True

        if i < m:
            continue

        return False

    return False


print(('Yes' if check_wifi(map_x) or check_wifi(map_y) else 'No'))
# Original solution
a = [list(map(int, input().split())) for _ in range(3)]
n = int(eval(input()))
for _ in range(n):
    b = int(eval(input()))
    for i in range(9):
        if b == a[i // 3][i % 3]:
            a[i // 3][i % 3] = 0
flag = False
if a[0][0] == a[0][1] == a[0][2] == 0:
    flag = True
if a[1][0] == a[1][1] == a[1][2] == 0:
    flag = True
if a[2][0] == a[2][1] == a[2][2] == 0:
    flag = True
if a[0][0] == a[1][0] == a[2][0] == 0:
    flag = True
if a[0][1] == a[1][1] == a[2][1] == 0:
    flag = True
if a[0][2] == a[1][2] == a[2][2] == 0:
    flag = True
if a[0][0] == a[1][1] == a[2][2] == 0:
    flag = True
if a[0][2] == a[1][1] == a[2][0] == 0:
    flag = True
if flag:
    print('Yes')
else:
    print('No')


# Optimized solution
a = [list(map(int, input().split())) for _ in range(3)]
n = int(eval(input()))
for _ in range(n):
    b = int(eval(input()))
    for i in range(9):
        if b == a[i // 3][i % 3]:
            a[i // 3][i % 3] = 0
flag = True
if a[0][0] == a[0][1] == a[0][2] == 0:
    flag = False
if a[1][0] == a[1][1] == a[1][2] == 0:
    flag = False
if a[2][0] == a[2][1] == a[2][2] == 0:
    flag = False
if a[0][0] == a[1][0] == a[2][0] == 0:
    flag = False
if a[0][1] == a[1][1] == a[2][1] == 0:
    flag = False
if a[0][2] == a[1][2] == a[2][2] == 0:
    flag = False
if a[0][0] == a[1][1] == a[2][2] == 0:
    flag = False
if a[0][2] == a[1][1] == a[2][0] == 0:
    flag = False
print(('YNeos' [flag::2]))
# Original solution
A = [list(map(int,input().split())) for _ in range(3)]
N = int(eval(input()))
B = [int(eval(input())) for _ in range(N)]

for b in B:
  for i in range(3):
    for j in range(3):
      if A[i][j] == b:
        A[i][j] = -100

f = False
for i in range(3):
  s = 0
  for j in range(3):
    s += A[i][j]
  if s == -300:
    f = True
for i in range(3):
  s = 0
  for j in range(3):
    s += A[j][i]
  if s == -300:
    f = True
s = 0
for i in range(3):
  s += A[i][i]
  if s == -300:
    f = True
s = 0
for i in range(3):
  s += A[i][2-i]
  if s == -300:
    f = True
print(('Yes' if f else 'No'))


# Optimized solution
A = [list(map(int,input().split())) for _ in range(3)]
N = int(eval(input()))
B = [int(eval(input())) for _ in range(N)]

for b in B:
  for i in range(3):
    for j in range(3):
      if A[i][j] == b:
        A[i][j] = 0
f = False
for i in range(3):
  s = 0
  for j in range(3):
    s += A[i][j]
  if s == 0:
    f = True
for i in range(3):
  s = 0
  for j in range(3):
    s += A[j][i]
  if s == 0:
    f = True
s = 0
for i in range(3):
  s += A[i][i]
if s == 0:
  f = True
s = 0
for i in range(3):
  s += A[i][2-i]
if s == 0:
  f = True
print(('Yes' if f else 'No'))
# Original solution
import sys
from pprint import pprint

bingo = [[] for _ in range(3)]
for i in range(3):
    bingo[i] = list(map(int, sys.stdin.readline().strip().split()))

N = int(sys.stdin.readline().strip())
B = set()
for _ in range(N):
    B.add(int(sys.stdin.readline().strip()))

bingo_flag = False
for i in range(3):
    tmp = []
    for j in range(3):
        if bingo[i][j] not in B:
            break
    else:
        bingo_flag = True

for i in range(3):
    for j in range(3):
        if bingo[j][i] not in B:
            break
    else:
        bingo_flag = True

for i in range(3):
    if bingo[i][i] not in B:
        break
else:
    bingo_flag = True

for i in range(3):
    if bingo[2-i][i] not in B:
        break
else:
    bingo_flag = True


if bingo_flag:
    print("Yes")
else:
    print("No")

# Optimized solution
import sys

input = sys.stdin.readline

bingo = []
for _ in range(3):
    bingo.append(list(map(int, input().split())))

N = int(eval(input()))
nums = []
for _ in range(N):
    nums.append(int(eval(input())))

for num in nums:
    for i in range(3):
        for j in range(3):
            if bingo[i][j] == num:
                bingo[i][j] = -1

for i in range(3):
    if sum(bingo[i]) == -3:
        print("Yes")
        sys.exit()
    tmp = 0
    for j in range(3):
        tmp += bingo[j][i]
    if tmp == -3:
        print("Yes")
        sys.exit()
if bingo[0][0] == -1 and bingo[1][1] == -1 and bingo[2][2] == -1:
    print("Yes")
    sys.exit()
if bingo[0][2] == -1 and bingo[1][1] == -1 and bingo[0][2] == -1:
    print("Yes")
    sys.exit()

print("No")
# Original solution
def main():
    top = list(map(int, input().split()))
    middle = list(map(int, input().split()))
    bottom = list(map(int, input().split()))
    N = int(eval(input()))
    taken = []
    for f in range(N):
        taken.append(int(eval(input())))
    
    bingo = []
    bingo.append(top)
    bingo.append(middle)
    bingo.append(bottom)
    temp = [top[0], middle[0], bottom[0]]
    bingo.append(temp)
    temp = [top[1], middle[1], bottom[1]]
    bingo.append(temp)
    temp = [top[2], middle[2], bottom[2]]
    bingo.append(temp)
    temp = [top[0], middle[1], bottom[2]]
    bingo.append(temp)
    temp = [top[2], middle[1], bottom[0]]
    bingo.append(temp)
    
    if bingo[0][0] in taken and bingo[0][1] in taken and bingo[0][2] in taken:
        print('Yes')
    elif bingo[1][0] in taken and bingo[1][1] in taken and bingo[1][2] in taken:
        print('Yes')
    elif bingo[2][0] in taken and bingo[2][1] in taken and bingo[2][2] in taken:
        print('Yes')
    elif bingo[3][0] in taken and bingo[3][1] in taken and bingo[3][2] in taken:
        print('Yes')
    elif bingo[4][0] in taken and bingo[4][1] in taken and bingo[4][2] in taken:
        print('Yes')
    elif bingo[5][0] in taken and bingo[5][1] in taken and bingo[5][2] in taken:
        print('Yes')
    elif bingo[6][0] in taken and bingo[6][1] in taken and bingo[6][2] in taken:
        print('Yes')
    elif bingo[7][0] in taken and bingo[7][1] in taken and bingo[7][2] in taken:
        print('Yes')
    else:
        print('No')
main()

# Optimized solution
def main():
    top = list(map(int, input().split()))
    middle = list(map(int, input().split()))
    bottom = list(map(int, input().split()))
    N = int(eval(input()))
    taken = []
    for i in range(N):
        taken.append(int(eval(input())))
    
    bingo = []
    bingo.append(top)
    bingo.append(middle)
    bingo.append(bottom)
    temp = [top[0], middle[0], bottom[0]]
    bingo.append(temp)
    temp = [top[1], middle[1], bottom[1]]
    bingo.append(temp)
    temp = [top[2], middle[2], bottom[2]]
    bingo.append(temp)
    temp = [top[0], middle[1], bottom[2]]
    bingo.append(temp)
    temp = [top[2], middle[1], bottom[0]]
    bingo.append(temp)
 
 
    cnt = 0
    flag = 0
    for i in range(len(bingo)):
        cnt = 0
        for j in range(len(bingo[i])):
            if bingo[i][j] in taken:
                cnt += 1
        if cnt == 3:
            flag += 1
    
    if flag > 0:
        print('Yes')
    else:
        print('No')
main()
# Original solution
A = []
 
for i in range(3):
    A.append(list(map(int, input().split())))
N = int(eval(input()))
b = []
for i in range(N):
    b.append(int(eval(input())))
 
for i in range(3):
    if A[i][0] in b and A[i][1] in b and A[i][2] in b:
        print('Yes')
        exit()
for i in range(3):
    if A[0][i] in b and A[1][i] in b and A[2][i] in b:
        print('Yes')
        exit()
if A[0][0] in b and A[1][1] in b and A[2][2] in b:
    print('Yes')
    exit()
if A[2][0] in b and A[1][1] in b and A[0][2] in b:
    print('Yes')
    exit()
 
 
 
print('No')


# Optimized solution
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
a3 = list(map(int, input().split()))
n = int(eval(input()))
a = []
ans = 0
for i in range(n):
    b = int(eval(input())) 
    a.append(b)
for i in range(3):
    if a1[i] in a and a2[i] in a and a3[i] in a:
        ans = 'Yes'
if a1[0] in a and a1[1] in a and a1[2] in a:
    ans = 'Yes'
if a2[0] in a and a2[1] in a and a3[2] in a:
    ans = 'Yes'
if a3[0] in a and a3[1] in a and a3[2] in a:
    ans = 'Yes'
if a1[0] in a and a2[1] in a and a3[2] in a:
    ans = 'Yes'
if a1[2] in a and a2[1] in a and a3[0] in a:
    ans = 'Yes'
if ans == 'Yes':
    print(ans)
else:
    print('No')