#include <bits/stdc++.h>
#define N 105
#define T 1115
using namespace std;
int w,h,scr[T][N],f[T][N],ts;
int nxt[T][N];
int dp(int a,int b){
if(a>ts) return 0;
if(~f[a][b]) return f[a][b];
int r=0;int n=0;
for(int i=-2; i<=2; ++i){
if(b+i<1||b+i>w) continue;
int tmp=dp(a+1,b+i)+scr[a+1][b+i];
if(tmp>r)r=tmp,n=i;
}
f[a][b]=r;
nxt[a][b]=n;
return r;
}
void dfs(int a,int b){
if(a>ts) return;
if(!f[a][b]) return;
cout<<nxt[a][b];
dfs(a+1,b+nxt[a][b]);
}
int main( ){
cin>>w>>h;
int a,b,c,d;
while(cin>>a>>b>>c>>d){
if((h-1)%c==0||a==0){
int t=(h-1)/c+a;
ts=max(ts,t);
scr[t][b]+=d;
}
}
memset(f,-2,sizeof(f));
int ans=dp(0,(w+1)*2);
cout<<ans;
dfs(0,(w+1)*2);
return 0;
}