9 13
6 14 19 5 -5 6 -10 20 8
 8 9
6 -1 5 -5 -8 -7 -8 -7
 10 7
-9 3 -4 -22 4 -17 0 -14 3 -2
 6 2
-2 3 0 -2 0 0
 5 10
-8 -24 0 -22 12
 5 13756
-2 -9 -10 0 10
 7 3
1 -3 0 3 -1 0 2
 9 9
-3 2 0 -2 -7 -1 0 5 3
 2 3
2 0
 19 78701
1 0 -1 0 -1 -1 0 1 0 -1 1 1 -1 1 0 0 -1 0 0
 5 4
-1 0 0 1 -1
 6 4
-1 0 2 -4 0 5
 20 23036
-1 1 -1 -1 -1 -1 1 -1 -1 0 0 1 1 0 0 1 0 0 -1 -1
 5 4
-1 0 -3 0 3
 12 82016
1 -2 -1 -1 -2 -1 0 -2 -1 1 -2 2
 7 4
-6 0 2 -3 0 4 0
 4 4
2 2 0 1
 6 1
-3 0 0 0 -2 3
 8 26
-4 9 -14 -11 0 7 23 -15
 20 23079
0 1 1 -1 1 0 -1 -1 0 0 1 -1 1 1 1 0 0 1 0 1
 1 1
1
 7 8555
-2 -3 -2 3 0 -2 0
 4 100
-100 0 -50 100
 3 14
12 12 -8
 1 1
2
 10 23
9 7 14 16 -13 -22 24 -3 -12 14
 8 11
12 -12 -9 3 -22 -21 1 3
 19 49926
-2 0 2 0 0 -2 2 -1 -1 0 0 0 1 0 1 1 -2 2 2
 16 76798
-1 11 -7 -4 0 -11 -12 3 0 -7 6 -4 8 6 5 -10
 9 5
-2 0 3 -4 0 4 -3 -2 0

-1
 -1
 1
 1
 1
 1
 -1
 2
 0
 1
 1
 -1
 1
 1
 1
 1
 -1
 1
 -1
 0
 0
 1
 1
 -1
 -1
 -1
 -1
 1
 1
 1

#from sys import setrecursionlimit
#setrecursionlimit(10**7)
n,d=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
c=[0]
i0=0
while a[i0]==0:
    i0=i0+1
for i in range(i0,n):
    if a[i]==0:
        if a[i-1]!=0:
            b.append((c[-1],d-max(c)))
            c=[0]
    else:
        c.append(c[-1]+a[i])
x,y=0,d
if a[-1]!=0:
    b.append((0,d-max(c)))
x=[]
z=0
z0=0
for i00 in range(len(b)):
    i=b[i00]
    if i[1]<0:
        print(-1)
        exit()
    z=z+i[0]
    b0=len(x)==2
    if b0:
        x0=max(x[0],min(0,x[1]))
        if z+x[0]>d:
            print(-1)
            exit()
        if z+x[0]<0:
            x=[max(x[0],-z),min(x[1],i[1]-z)]
        else:
            x[1]=min(x[1],i[1]-z)
        if x[0]==x[1]:
            z=z+x[0]
            x=[]
            continue
        if x[0]>x[1] and b0 and x0>0:
            z=z+x0
            x=[]
        else:
            print(-1)
            exit()
    else:
        if z>d:
            print(-1)
            exit()
        if z<0:
            if i00==len(b)-1:
                x=[-z,d-z]
            else:
                x=[-z,min(d-z,b[i00+1][1]-z)]
            z0=z0+1
            if x[0]>x[1]:
                print(-1)
                exit()
            if x[0]==x[1]:
                z=z+x[0]
                x=[]







'''
5 10
-1 5 0 -5 3

3 4
-10 0 20

5 10
-5 0 10 -11 0

'''
print(z0)
