n=int(input())
m=int(input())
a=int(input())
b=int(input())
c=int(input())
d=int(input())
e=0
y=0
lst1=[a,b,c,d]
for i in range(4):
    if 0 in lst1[i]:
        for j in range(4):
            if j == 0:
                y=j
                break
for i in range(4-i):
    if 0 in lst1[i]:
        for j in range(4):
            if j == 0:
                e=j
                break
print(16-(e+y-1))