{$o-,q+,r+}
{$apptype console}
{$MINSTACKSIZE $00400000}

uses
    math, sysutils;

type
    int = longint;

const
    maxn = 100000;
    maxv = 1000000;

type
    tarr = array[1 .. maxn] of int;    
procedure qs(var a: tarr; l, r: int);
var
    i, j, x, t: int;
begin
    if (l >= r) then
        exit;
    i := l;
    j := r;
    x := a[random(r - l + 1) + l];
    repeat
        while (a[i] > x) do
            inc(i);
        while (x > a[j]) do
            dec(j);
        if (i <= j) then begin
            t := a[i];
            a[i] := a[j];
            a[j] := t;
            inc(i);
            dec(j);
        end;
    until i > j;
    qs(a, i, r);
    qs(a, l, j);
end;
var
    testn: int;
procedure writeTest(n: int; var p, v: tarr);
var
    i: int;
    s: string;
begin
    inc(testn);
    s := inttostr(testn);
    if (testn < 10) then
        s := '0' + s;
    rewrite(output, s);    

    writeln(n);
    for i := 1 to n do
        writeln(v[i], ' ', p[i]);

    close(output);    
end;


procedure doRandom(n, minV, maxV, minP, maxP: int; nonSelling: extended);
var
    i: int;
    p, v: tarr;
begin
    for i := 1 to n do begin
	v[i] := random(maxV - minV + 1) + minV;	
        if (random > nonSelling) then
            p[i] := random(maxP - minP + 1) + minP
        else
            p[i] := -1;
    end;
    writeTest(n, p, v);
end;

procedure genPerm(n: int; var p: tarr);
var
    i, j: int;
begin
    for i := 1 to n do begin
        j := random(i - 1) + 1;
        p[i] := p[j];
        p[j] := i;
    end;
end;
procedure shuffle1(n: int; var p: tarr);
var
    r, c: tarr;
    i: int;
begin
    genPerm(n, r);
    for i := 1 to n do
        c[i] := p[r[i]];
    for i := 1 to n do
        r[i] := c[i];
end;
procedure shuffle(n: int; var p, v: tarr);
begin
    shuffle1(n, p);
    shuffle1(n, v);
end;

procedure doCloseAnswers(n, maxValue, maxDeviation: int);
var
    p, v: tarr;
    ps: array[1 .. maxn] of int64;
    cost: array[1 .. maxn] of int64;

    i: int;

    cutLevel: int64;
    lastCut: int64;
    maxCost: int64;

begin
    for i := 1 to n do
        v[i] := random(maxValue) + 1;
    qs(v, 1, n);
    ps[1] := 0;
    for i := 2 to n do
        ps[i] := ps[i - 1] + v[i - 1];

    maxCost := 0;

    cutLevel := v[1] + 1;
    lastCut := 0;
    for i := 1 to n do begin
        while (cutLevel >= v[i] + ps[lastCut + 1] - lastCut * cutLevel) do begin
            dec(cutLevel);
            while (lastCut < i - 1) and (v[lastCut + 1] >= cutLevel) do
                inc(lastCut);
        end;
        cost[i] := ps[lastCut + 1] - lastCut * cutLevel;
        maxCost := max(maxCost, cost[i]);
    end;

    inc(maxCost, maxDeviation + 1);
    for i := 1 to n do begin
        p[i] := max(0, maxCost - cost[i] + random(2 * maxDeviation + 1) - maxDeviation);
    end;
    shuffle(n, p, v);
    writeTest(n, p, v);
end;

procedure doMax12and3();
var
    i, j, n: int;
    p, v: tarr;
begin
    n := maxn;    
    for i := 1 to n do begin
        p[i] := -1;
        v[i] := maxv;
    end;
    for j := 0 to 2 do begin
        for i := 1 to max(1, j * (n div 2)) do begin
            p[i] := maxv;
            v[i] := 1;
        end;
        writeTest(n, p, v);
    end;    
end;
procedure doMax4();
var
    i, n: int;
    p, v: tarr;
begin
    n := maxn;    
    for i := 1 to n do begin
        p[i] := maxv;
        v[i] := maxv;
    end;  
    writeTest(n, p, v);
end;

var
    i, j, k: int;
begin
    testn := strtoint(paramstr(1));
    randseed := strtoint(paramstr(2));


    doRandom(5, 1, 5, 1, 5, 0.25);

    doRandom(10, 1, 5, 1, 5, 0.25);
    doRandom(50, 1, 5, 1, 5, 0.5);
    for i := 5 to 8 do
        for j := 5 to 8 do
            for k := 0 to 3 do begin
                if (testn = 10) then begin
                    doRandom(maxn, 1, 10, maxv - 10, maxv, 0.05);
                end;
                if (random(5) > 1) then
                    doRandom((i + j) * 10, 2 * 10, (2 + i) * 10, 2 * 10, (2 + j) * 10, (5 + 20 * k) * 0.01);
            end;

    doMax12and3();
    doMax4();

    doCloseAnswers(100, maxv, 0);
    doCloseAnswers(100, maxv, 1);
    doCloseAnswers(100, maxv, 2);
    doCloseAnswers(maxn, maxv, 0);
    doCloseAnswers(maxn, maxv, 1);
    doCloseAnswers(maxn, maxv, 2);
    doCloseAnswers(maxn, maxv, 10);

    doRandom(maxn, 1, 10, 1, 10, 0.999);
    doRandom(maxn, maxv - 10, maxv, 1, 10, 0.3);
    doRandom(maxn, maxv - 10, maxv, maxv - 10, maxv, 0.5);

    for k := 0 to 3 do
        doRandom(maxn, 1, maxv, 1, maxv, k * 0.25);
end.