#include<bits/stdc++.h>
using namespace std;
const int N=100005,INF=1062000000;
struct Ask{
int x,y,p,q;
}A[N*2];
int dp[N][1005],n,m,a,b,c;
inline int solve(int x){
return a*x*x+b*x+c;
}
inline int cmp(Ask x,Ask y){
return x.p<y.p;
}
inline int read(){
int x=0;char c=0;
for (;c<'0'||c>'9';c=getchar());
for (;c>='0'&&c<='9';c=getchar())x=x*10+c-48;
return x;
}
int main(){
n=read();m=read();a=read();b=read();c=read();
for (int i=1;i<=m;i++)
A[i].x=read(),A[i].y=read(),A[i].p=read(),A[i].q=read();
sort(A+1,A+m+1,cmp);
int T=1000;
for (int i=1;i<=n;i++)
for (int j=0;j<=T;j++)dp[i][j]=INF;
for (int i=0;i<=T;i++)dp[1][i]=solve(i);
for (int i=1;i<=m;i++)
for (int j=A[i].q;j<=T;j++)
dp[A[i].y][j]=min(dp[A[i].y][j],dp[A[i].x][A[i].p]+
solve(j-A[i].q));
int Ans=INF+T;
for (int i=0;i<=T;i++)Ans=min(Ans,dp[n][i]+i);
printf("%d\n",Ans-c);
}