d[99],a[5],i=1<<20,t=31,m=57,*p;main(j){for(;~scanf("%d",a+j++););for(;--i;m=m>t?t:m){for(;--t;d[t]=9);for(;--j?*(p=d+t+(i>>5*j&31))>d[t]?*p=d[t]+1:1:(j=4*(t++<31)););for(;++j<5;t+=d[a[j]]+4);}t=!puts(&m);}