PROGRAM stafeta;
USES crt;
VAR
	k,n,futo: INTEGER;	//tav, futok szama, hasznalt futok szama
	s,v: ARRAY[1..20000] OF INTEGER;	//start, vegzodes
	fsr: ARRAY[1..1000] OF INTEGER;	//milyen sorban futnak

PROCEDURE beolvas(honnan: STRING);
	VAR
		f: TEXT;
		i: INTEGER;
	BEGIN
		ASSIGN(f,honnan);
		RESET(f);
		READ(f,k);
		READ(f,n);
		FOR i:=1 TO n DO
			BEGIN
				READ(f,s[i]);
				READ(f,v[i]);
			END;
		CLOSE(f);
	END;

PROCEDURE megold;
	VAR
		i,ma,sr,hol: INTEGER;	//aktualis maximum, a hozza tartozo sorszam, hol tartunk
	BEGIN
		futo:=0;
		hol:=0;
		WHILE NOT(hol>=k) DO
			BEGIN
				ma:=hol;
				sr:=0;
				FOR i:=1 TO n DO
					BEGIN
						IF ((s[i]<=hol) AND (v[i]>ma)) THEN
							BEGIN
								ma:=v[i];
								sr:=i;
							END;
					END;
				hol:=ma;
				futo:=futo+1;
				fsr[futo]:=sr;
			END;
		WRITELN(futo);
	END;

PROCEDURE kiiras(hova: STRING);
	VAR
		f: TEXT;
		i: INTEGER;
	BEGIN
		ASSIGN(f,hova);
		REWRITE(f);
		WRITELN(f,futo);
		FOR i:=1 TO futo DO
			BEGIN
				WRITE(f,fsr[i],' ');
			END;
		CLOSE(f);
	END;

PROCEDURE foprogram;
	BEGIN
		beolvas('stafeta.be7');
		megold;
		kiiras('stafetaki.txt');
	END;

BEGIN
	foprogram;
END.
