program pil_15_27(input1,input2,output1);
type
	FI = file of integer;
var
	f, g, h : FI;

procedure merge(var f, g, h : FI);
var
	endf, endg : boolean;
	curf, curg : integer;
begin
	reset(f);
	reset(g);
	rewrite(h);
	read(f, curf);
	read(g, curg);
	endf := eof(f);
	endg := eof(g);
	while not (endf or endg) do begin
		while (curf<curg) and (not endf) do begin
			write(h, curf);
			read(f, curf);
			endf := eof(f);
		end;
		while (curg<curf) and (not endg) do begin
			write(h, curg);
			read(g, curg);
			endg := eof(g);
		end;
	end;
	if endf then begin
		write(h, curf, curg);
		while not eof(g) do begin
			read(g, curg);
			write(h, curg);
		end;
	end
	else begin
		while not eof(f) and (curf<curg) do begin
			write(h, curf);
			read(f, curf);
		end;
		write(h, curg, curf);
		while not eof(f) do begin
			read(f, curf);
			write(h, curf);
		end;
	end;
end;

begin
	assign(f, 'input1');
	assign(g, 'input2');
	assign(h, 'output1');
	merge(f, g, h);
end.
