import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class test4 {
static int kd, gd, qs, end = -1, ans[] = new int[1005], l, r;
static long fs[][] = new long[1005][100], dp[][] = new long[1005][100];
static long dpjs(int t, int wz) {
if (wz < l || wz > r)// 防止数组越界
return 0;
else {
return dp[t][wz];
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
kd = nextint();
qs = kd / 2 + 1;
gd = nextint();
while (hasnext()) {
int t0 = nextint();
int hzb = nextint();
int v = nextint();
long score = nextlong();
if ((gd - 1) % v != 0 || v == 0)// 不是恰好某一秒末尾结束
continue;
int t1 = (gd - 1) / v + t0;
end = Math.max(end, t1);// 总时间
fs[t1][hzb] += score;// t1秒hzb位置的分数
}
for (int i = 1; i <= end; i++) {
l = Math.max(qs - 2 * i, 1);// 左边最远
r = Math.min(qs + 2 * i, kd);// 右边最远
for (int p = l; p <= r; p++) {
for (int q = -2; q <= 2; q++) {
dp[i][p] = Math.max(dp[i][p], dpjs(i - 1, p - q) + fs[i][p]);// dp
}
}
}
long jg = -1;
l = Math.max(qs - 2 * end, 1);
r = Math.min(qs + 2 * end, kd);
for (int i = l; i <= r; i++) {
if (jg < dp[end][i]) {
jg = dp[end][i];
}
}
println(jg);
int cs = end - 1;
for (int i = end; i >= 1; i--) {
l = Math.max(qs - 2 * i, 1);
r = Math.min(qs + 2 * i, kd);
for (int p = l; p <= r; p++) {
if (dp[i][p] == jg) {
ans[cs--] = p;// 倒着记录hzb位置
jg -= fs[i][p];
break;
}
}
}
for (int i = 0; i < end; i++) {
println(ans[i] - qs);// 横坐标推移动
qs = ans[i];
}
flsh();
}
//快读
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
static String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
String lineString = br.readLine();
st = new StringTokenizer(lineString);
}
return st.nextToken();
}
static boolean hasnext() throws IOException {
if (st != null && st.hasMoreElements()) {
return false;
}
br.mark(1024);
String s = br.readLine();
br.reset();
return s != null && !s.isEmpty();
}
static int nextint() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
static long nextlong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
static double nextdouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
static String nextline() throws IOException {
return br.readLine();// ??
}
static void println(Object o) {
pw.println(o);
}
static void println() {
pw.println();
}
static void printf(String s, Object... o) {
pw.printf(s, o);
}
static void flsh() {
pw.flush();
}
}