#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define pa pair<int,int>
#define N 7700
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x;
}
int num=1,h[N],f[N],flag[N],pre[N],path[N],T,n,m,win[N],lose[N];
struct node{
int y,z,next,c;
}data[2200000];
inline void insert1(int x,int y,int z,int c){
data[++num].y=y;data[num].z=z;data[num].c=c;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=0;data[num].c=-c;data[num].next=h[y];h[y]=num;
}
inline bool spfa(){
memset(f,0x3f,sizeof(f));memset(flag,0,sizeof(flag));memset(pre,-1,sizeof(pre));f[0]=0;flag[0]=1;queue<int>q;q.push(0);
while(!q.empty()){
int x=q.front();q.pop();flag[x]=0;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z,c=data[i].c;
if (f[x]+c<f[y]&&z){
f[y]=f[x]+c;pre[y]=x;path[y]=i;
if (!flag[y]) flag[y]=1,q.push(y);
}
}
}if (pre[T]==-1) return 0;else return 1;
}
int x[N],y[N],c[N],d[N],ans;
int main(){
freopen("bzoj1449.in","r",stdin);
n=read();m=read();T=n+m+1;
for (int i=1;i<=n;++i) win[i]=read(),lose[i]=read(),c[i]=read(),d[i]=read();
for (int i=1;i<=m;++i) x[i]=read(),y[i]=read(),++lose[x[i]],++lose[y[i]];
for (int i=1;i<=n;++i) ans+=c[i]*win[i]*win[i]+d[i]*lose[i]*lose[i];//printf("%d\n",ans);
for (int i=1;i<=m;++i){
insert1(0,i,1,0);insert1(i,x[i]+m,1,0);insert1(i,y[i]+m,1,0);
insert1(x[i]+m,T,1,2*c[x[i]]*win[x[i]]-2*d[x[i]]*lose[x[i]]+c[x[i]]+d[x[i]]);++win[x[i]];--lose[x[i]];
insert1(y[i]+m,T,1,2*c[y[i]]*win[y[i]]-2*d[y[i]]*lose[y[i]]+c[y[i]]+d[y[i]]);++win[y[i]];--lose[y[i]];
}
while(spfa()){
int minn=inf,now=T;
while(now) minn=min(minn,data[path[now]].z),now=pre[now];now=T;
while(now) {ans+=data[path[now]].c*minn;data[path[now]].z-=minn;data[path[now]^1].z+=minn;now=pre[now];}
}printf("%d",ans);
return 0;
}