n=int(input())
A=[]
D=[]
A0=[int(x) for x in input().split() ]
ans=0
for i in range(n):
    A.append([int(x) for x in input().split() ])
    d1=max(A[i][1] - A0[2], 0)
    d2=max(A0[1] - A[i][2], 0)
    if d2==0:
        if d1 > 0:
            ans=-1
            break
        else:
            timetokill=0
    else:
        timetokill=A[i][0]//d2 + ( A[i][0]%d2 != 0 ) - ( A0[3] > A[i][3] )
    D.append([d1,timetokill])
D.sort(key=lambda x: x[0], reverse=True)
pasttime=0
if ans==0:
    for d in D:
        pasttime += d[1]
        ans += d[0]*pasttime
if ans > A0[0]:
    ans=-1
print (ans)

