算法:DP
program vijos1246;
const
maxn=100000;
var
n,m:longint;
f:array [0..maxn,0..5] of longint;{采用了类似于求余数的方式,二维的0..5表示当前这一轮的总和除以6的余数,因此余数为0,1,2都可以成立。}
can:array [0..maxn] of boolean;
start:array [0..maxn] of longint;
function max(x,y:longint):longint;
begin
if x>y then exit(x) else exit(y);
end;
procedure init;
var
i,j,mh,gang,st,ed:longint;
s,ts:string;
begin
readln(n,m);
for i:=1 to m do
begin
readln(s);
mh:=pos(':',s);
gang:=pos('-',s);
ts:=copy(s,mh+1,gang-mh-1);
val(ts,st);
ts:=copy(s,gang+1,length(s)-gang);
val(ts,ed);
if ed>start[st] then start[st]:=ed;{表示st这个内容最晚哪会完成。}
end;
for i:=1 to n do
begin
for j:=i to start[i]-1 do
can[j]:=true;{凡是必须一同完成的标记一下。}
end;
end;
procedure main;
var
i,j:longint;
begin
fillchar(f,sizeof(f),255);
f[0,1]:=0;
f[0,0]:=0;
for i:=1 to n do
begin
f[i,0]:=f[i-1,5];
if not can[i-1] then f[i,1]:=max(f[i-1,0],max(f[i-1,1],f[i-1,2]))+1 else f[i,1]:=f[i-1,0];{如果可以不一同完成就可以拆分。}
for j:=2 to 5 do f[i,j]:=f[i-1,j-1];{因为除以6余数只有0~5,所以必然在某个阶段产生循环。}
end;
end;
procedure outit;
begin
if max(f[n,0],max(f[n,1],f[n,2]))>=0 then writeln(max(f[n,0],max(f[n,1],f[n,2]))) else writeln(-1);
end;
begin
assign(input,'VJ1246.in'); reset(input);
assign(output,'VJ1246.out'); rewrite(output);
init;
main;
outit;
close(input); close(output);
end.