You are given an array $a$ consisting of $n$ integers. Each $a_i$ is one of the six following numbers: $4, 8, 15, 16, 23, 42$.

Your task is to remove the minimum number of elements to make this array good.

An array of length $k$ is called good if $k$ is divisible by $6$ and it is possible to split it into $\frac{k}{6}$ subsequences $4, 8, 15, 16, 23, 42$.

Examples of good arrays:  $[4, 8, 15, 16, 23, 42]$ (the whole array is a required sequence);  $[4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42]$ (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements);  $[]$ (the empty array is good). 

Examples of bad arrays:   $[4, 8, 15, 16, 42, 23]$ (the order of elements should be exactly $4, 8, 15, 16, 23, 42$);  $[4, 8, 15, 16, 23, 42, 4]$ (the length of the array is not divisible by $6$);  $[4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23]$ (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence). 


-----Input-----

The first line of the input contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of elements in $a$.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ (each $a_i$ is one of the following numbers: $4, 8, 15, 16, 23, 42$), where $a_i$ is the $i$-th element of $a$.


-----Output-----

Print one integer — the minimum number of elements you have to remove to obtain a good array.


-----Examples-----
Input
5
4 8 15 16 23

Output
5

Input
12
4 8 4 15 16 8 23 15 16 42 23 42

Output
0

Input
15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42

Output
3

Difficulty: introductory

Starter code:

Solutions:
#!/usr/bin/env python
n = int(input())
a = list(map(int, input().split()))
k = [None, 4, 8, 15, 16, 23, 42]
s = [n, 0, 0, 0, 0, 0, 0]
for ai in a:
    for i in range(6, 0, -1):
        if ai == k[i] and s[i - 1] > 0:
            s[i] += 1; s[i - 1] -= 1
            break
print(n - 6 * s[-1])


from sys import stdin
n=int(stdin.readline().strip())
dp=[0 for i in range(6)]
d={4: 0, 8: 1, 15: 2, 16: 3, 23: 4, 42: 5}
s=list(map(int,stdin.readline().strip().split()))
for i in range(n-1,-1,-1):
    if s[i]==42:
        dp[5]+=1
        continue
    elif dp[d[s[i]]+1]>dp[d[s[i]]]:
        dp[d[s[i]]]+=1
print(n-6*dp[0])


n=int(input())
l1=list(map(int,input().split()))
x=[4,8,15,16,23,42]
count=[0]*6
for item in l1:
    if item==4:
        count[0]+=1
    elif item==8:
        if count[0]>count[1]:
            count[1]+=1
    elif item==15:
        if count[1]>count[2]:
            count[2]+=1
    elif item==16:
        if count[2]>count[3]:
            count[3]+=1
    elif item==23:
        if count[3]>count[4]:
            count[4]+=1
    elif item==42:
        if count[4]>count[5]:
            count[5]+=1
print(n-6*count[5])


n = int(input())
a = [int(s) for s in input().split(' ')]
col = 0
b = [0, 0, 0, 0, 0, 0]
for el in a:
    if el == 4:
        b[0] += 1
    elif el == 8:
        if b[0] > 0:
            b[0] -= 1
            b[1] += 1
        else:
            col += 1
    elif el == 15:
        if b[1] > 0:
            b[1] -= 1
            b[2] += 1
        else:
            col += 1
    elif el == 16:
        if b[2] > 0:
            b[2] -= 1
            b[3] += 1
        else:
            col += 1
    elif el == 23:
        if b[3] > 0:
            b[3] -= 1
            b[4] += 1
        else:
            col += 1
    elif el == 42:
        if b[4] > 0:
            b[4] -= 1
            b[5] += 1
        else:
            col += 1
    else:
        col += 1
col += b[0] + 2*b[1] + 3*b[2] + 4*b[3] + 5*b[4]
print(col)

from collections import Counter

n = int(input().strip())
nums = list(map(int, input().strip().split()))

res = 0
state = []
counter = Counter()
prev = {
    8: 4,
    15: 8,
    16: 15,
    23: 16,
    42: 23,
}
good_nums = [4, 8, 15, 16, 23, 42]

for i, num in enumerate(nums):
    if num == 4:
        counter[num] += 1
    elif counter[num] < counter[prev[num]]:
        counter[num] += 1
        if num == 42:
            for good_num in good_nums:
                counter[good_num] -= 1
    else:
        res += 1

res += sum(counter.values())

print(res)

n = int(input())   
cnt = {i: 0 for i in range(1, 7)}
cnt[0] = float('inf')
f = {x: i + 1 for i, x in enumerate((4, 8, 15, 16, 23, 42))}
for e in map(int, input().split()):
    e = f[e]
    if cnt[e - 1] > 0:
        cnt[e] += 1
        cnt[e - 1] -= 1
print(n - 6 * cnt[6])

D = {4:0, 8:1, 15:2, 16:3, 23:4, 42:5}
N = int(input())
A = [D[int(a)] for a in input().split()]
X = [0] * 6
for a in A:
    if a == 0:
        X[a] += 1
    else:
        if X[a-1]:
            X[a-1] -= 1
            X[a] += 1
print(N-X[5]*6)


n = int(input())
kol = [0, 0, 0, 0, 0, 0]
ind = [4, 8, 15, 16, 23, 42]
delete = 0
for i in map(int,input().split()):
    for j in range(6):
        if ind[j] == i:
            cur = j
            break
    if cur == 0:
        kol[0] += 1
    else:
        if kol[cur-1] != 0:
            kol[cur-1] -= 1
            kol[cur] += 1
        else:
            delete += 1
print(delete + kol[0] + kol[1]*2 + kol[2]*3 + kol[3]*4 + kol[4]*5)

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    used = [0 for i in range(n)]
    sh = [4, 8, 15, 16, 23, 42]
    time = 0
    arr = [0 for i in range(6)]
    for i in range(n):
        if a[i] == sh[0]:
            arr[0] += 1
        elif a[i] == sh[1]:
            if arr[0] > 0:
                arr[0] -= 1
                arr[1] += 1
        elif a[i] == sh[2]:
            if arr[1] > 0:
                arr[1] -= 1
                arr[2] += 1
        elif a[i] == sh[3]:
            if arr[2] > 0:
                arr[2] -= 1
                arr[3] += 1
        elif a[i] == sh[4]:
            if arr[3] > 0:
                arr[3] -= 1
                arr[4] += 1
        elif a[i] == sh[5]:
            if arr[4] > 0:
                arr[4] -= 1
                arr[5] += 1
    print(n - (arr[-1] * 6))


main()


n = int(input())
arr = list(map(int, input().split()))
_4, _8, _15, _16, _23, _42 = 0, 0, 0, 0, 0, 0

for i in arr:
  if i == 4:
    _4 += 1
  
  if i == 8 and _4 > _8:
    _8 += 1


  elif i == 15 and _8 > _15:
    _15 += 1

  elif i == 16 and _15 > _16:
    _16 += 1

  elif i == 23 and _16 > _23:
    _23 += 1

  elif i == 42 and _23 > _42:
    _42 += 1

print(n - _42 * 6)


import sys
input = sys.stdin.readline

n=int(input())
A=list(map(int,input().split()))

B=[]
C=[]
D=[]
E=[]
F=[]
G=[]

for i in range(n):
    if A[i]==4:
        B.append(i)

    if A[i]==8:
        C.append(i)

    if A[i]==15:
        D.append(i)

    if A[i]==16:
        E.append(i)

    if A[i]==23:
        F.append(i)

    if A[i]==42:
        G.append(i)

NOW=0
Bi=0
Ci=0
Di=0
Ei=0
Fi=0
Gi=0

ANS=0

while True:
    
    for b in range(Bi,len(B)):
        NOW=B[b]
        Bi=b+1
        break
    else:
        break

    #print("!")

    for c in range(Ci,len(C)):
        if C[c]>=NOW:
            NOW=C[c]
            Ci=c+1
            break
    else:
        break

    for d in range(Di,len(D)):
        if D[d]>=NOW:
            NOW=D[d]
            Di=d+1
            break
    else:
        break

    for e in range(Ei,len(E)):
        if E[e]>=NOW:
            NOW=E[e]
            Ei=e+1
            break
    else:
        break

    for f in range(Fi,len(F)):
        if F[f]>=NOW:
            NOW=F[f]
            Fi=f+1
            break
    else:
        break

    for g in range(Gi,len(G)):
        if G[g]>=NOW:
            NOW=G[g]
            Gi=g+1
            break
    else:
        break

    ANS+=6

print(len(A)-ANS)

    


input() # discard n
num = [4,8,15,16,23,42]
prev = {i:j for i,j in zip(num[1:], num)}
c = {v:0 for v in num}
n = 0
for a in map(int, input().split()):
    #print('before', a, c)
    if a == num[0]: # first
        c[a] += 1
    else:
        if c[prev[a]]:
            c[prev[a]] -= 1
            if a != num[-1]: c[a] += 1
        else: n += 1 # waste
    #print('after', a, c)

#print(c)
for i,v in enumerate(num):
    #print(i,v,c[v])
    n += (i+1)*c[v]

print(n)


n = int(input())
a = list(map(int, input().split()))

g = [0 for i in range(43)]
ans = 0

for i in a:
    if i == 4:
        g[4] += 1
    elif i == 8:
        if g[4] > 0:
            g[4] -= 1
            g[8] += 1
        else:
            ans += 1
    elif i == 15:
        if g[8] > 0:
            g[8] -= 1
            g[15] += 1
        else:
            ans += 1
    elif i == 16:
        if g[15] > 0:
            g[15] -= 1
            g[16] += 1
        else:
            ans += 1
    elif i == 23:
        if g[16] > 0:
            g[16] -= 1
            g[23] += 1
        else:
            ans += 1
    elif i == 42:
        if g[23] > 0:
            g[23]-=1
        else:
            ans += 1
    else:
        ans += 1
ans += (g[4] + 2*g[8] + 3*g[15] + 4*g[16]+ 5*g[23])
print(ans)

n = int(input())
s= list(map(int,input().split()))
count = [0]*6
for temp in s:
    if temp==4:
        count[0]+=1
    elif temp==8 and count[0]>count[1]:
        count[1]+=1
    elif temp==15 and count[1]>count[2]:
        count[2]+=1
    elif temp==16 and count[2]>count[3]:
        count[3]+=1
    elif temp==23 and count[3]>count[4]:
        count[4]+=1
    elif temp==42 and count[4]>count[5]:
        count[5]+=1
    else:
        pass
#print(count)
out = n - 6*min(count)
print(out)

n = int(input())
a = [int(x) for x in input().split()]
exist = [4,8,15,16,23,42]
s = set(exist)
waiting = [0] * 6
ans = 0
for x in a:
    if x not in s:
        ans += 1
        continue
    if x == 4:
        waiting[0] += 1
        continue
    pos = exist.index(x)
    if waiting[pos - 1] == 0:
        ans += 1
        continue
    waiting[pos - 1] -= 1
    if x != 42:
        waiting[pos] += 1
# print(waiting)
for i in range(6):
    ans += (i + 1) * waiting[i]
print(ans)
    


n = int(input())
x = [4, 8, 15, 16, 23, 42]
r = [0] * len(x)
z = 0
for v in map(int, input().split()):
    i = x.index(v)
    if not i or r[i-1] > r[i]:
        r[i] += 1
    else:
        z += 1
z += sum(v-r[-1] for v in r)
print(z)


n = int(input())
l = list(map(int, input().split()))
x = [4,8,15,16,23,42]
# if(n == 0):
# 	print(0)
# elif(n<6):
# 	print(n)
# elif(n==6):
# 	if(l==x):
# 		print(0)
# 	else:
# 		print(n)
# else:
d = {}
for i in x:
	d[i] = 0
count = 0
for i in range(n):
	if(l[i]==4):
		d[4]+=1
	elif(l[i]==8):
		if(d[4]==0):
			count+=1
		else:
			d[4]-=1
			d[8]+=1
	elif(l[i]==15):
		if(d[8]==0):
			count+=1
		else:
			d[8]-=1
			d[15]+=1
	elif(l[i]==16):
		if(d[15]==0):
			count+=1
		else:
			d[15]-=1
			d[16]+=1
	elif(l[i]==23):
		if(d[16]==0):
			count+=1
		else:
			d[16]-=1
			d[23]+=1
	else:
		if(d[23]==0):
			count+=1
		else:
			d[23]-=1
			d[42]+=1
count+= d[4] + (2*d[8]) + (3*d[15]) + (4*d[16]) + (5*d[23])
print(count)

import sys
from collections import Counter

def input():
    return sys.stdin.readline().strip()

def dinput():
    return int(input())

def tinput():
    return input().split()

def rinput():
    return map(int, tinput())

def main():
    n = int(input())
    aleonov = list(rinput())
    rt = [4, 8, 15, 16, 23, 42]
    leonov = []
    for i in range(6):
        leonov.append(0)
    for i in range(n):
        if aleonov[i] == rt[0]:
            leonov[0] += 1
        elif aleonov[i] == rt[1]:
            if leonov[0] > 0:
                leonov[0] -= 1
                leonov[1] += 1
        elif aleonov[i] == rt[2]:
            if leonov[1] > 0:
                leonov[1] -= 1
                leonov[2] += 1
        elif aleonov[i] == rt[3]:
            if leonov[2] > 0:
                leonov[2] -= 1
                leonov[3] += 1
        elif aleonov[i] == rt[4]:
            if leonov[3] > 0:
                leonov[3] -= 1
                leonov[4] += 1
        elif aleonov[i] == rt[5]:
            if leonov[4] > 0:
                leonov[4] -= 1
                leonov[5] += 1
    print(n - (leonov[-1] * 6))


main()

for _ in range(1):
    n=int(input())
    a=list(map(int,input().split()))
    d={}
    b=[4,8,15,16,23,42]
    c=[0]*50
    for i in a:
        if i==4:
            c[i]+=1
        elif c[b[b.index(i) - 1]]>0:
            c[b[b.index(i) - 1]]-=1
            c[i]+=1
            
    print(n- (c[42])*6)


n = int(input())
arr = list(map(int,input().split()))
ans = 0
counter = [0] * 6
d = {4:0,8:1,15:2,16:3,23:4,42:5}

for a in arr:
    index = d[a]
    counter[index] += 1
    if index > 0 and counter[index] > counter[index-1]:
        counter[index] -= 1
        ans += 1
        
ans += sum(counter) - counter[5] * 6

print(ans)


'''input
15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42
'''
# its just a simulation
from sys import stdin, setrecursionlimit
import heapq



def refine(arr):
	aux = []
	for i in range(len(arr)):
		if arr[i] in [4, 8, 15, 16, 23, 42]:
			aux.append(arr[i])
	return aux

# main starts
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
arr = refine(arr)
mydict = dict()
mydict[4] = 0; mydict[8] = 0; mydict[15] = 0; mydict[16] = 0;
mydict[23] = 0; mydict[42] = 0

for i in range(len(arr)):
	if arr[i] == 4:
		mydict[4] += 1
	elif arr[i] == 8:
		if mydict[4] >= mydict[8] + 1:
			mydict[8] += 1
	elif arr[i] == 15:
		if mydict[8] >= mydict[15] + 1:
			mydict[15] += 1

	elif arr[i] == 16:
		if mydict[15] >= mydict[16] + 1:
			mydict[16] += 1
	elif arr[i] == 23:
		if mydict[16] >= mydict[23] + 1:
			mydict[23] += 1
	elif arr[i] == 42:
		if mydict[23] >= mydict[42] + 1:
			mydict[42] += 1

print(n - mydict[42] * 6 )


from collections import defaultdict

MEMBERS = (4, 8, 15, 16, 23, 42)
progress = {4: 0, 8: 1, 15: 2, 16: 3, 23: 4, 42: 5}

n = int(input())
a = list(map(int, input().split()))
idx = []
counts = defaultdict(int)
for num in a:
    if progress[num] == 0:
        counts[0] += 1
    else:
        if counts[progress[num] - 1] > 0:
            counts[progress[num] - 1] -= 1
            counts[progress[num]] += 1
print(n - counts[5] * 6)


n = int(input())
a = list(map(int,input().split()))

ans = 0

chk = {4:0,8:1,15:2,16:3,23:4,42:5}

ct = [0]*6
for i in a:
    if i not in [4,8,15,16,23,42]:
       ans+=1
for i in a:
    if i in [4,8,15,16,23,42]:
       if i==4:
          ct[0]+=1
       else:
           if ct[chk[i]-1]>ct[chk[i]]:
              ct[chk[i]]+=1
           else:
              ans+=1
ans1 = ct[5]
for i in ct:
    ans += (i-ans1)
print(ans)

n = int(input())
a = list(map(int,input().split()))
dp = [0,0,0,0,0,0]
for k in range(n):
    if a[k] == 4:
        dp[0] += 1
    elif a[k] == 8:
        dp[1] = min(dp[0],dp[1]+1)
    elif a[k] == 15:
        dp[2] = min(dp[1],dp[2]+1)
    elif a[k] == 16:
        dp[3] = min(dp[2],dp[3]+1)
    elif a[k] == 23:
        dp[4] = min(dp[3],dp[4]+1)
    else:
        dp[5] = min(dp[4],dp[5]+1)

print(n-6*dp[5])