#!/usr/bin/perl -w
use strict;

print "m = n := ";
my $size = <STDIN>;
chomp($size);
my $N = $size * $size;
my %numbers = ();
my $increment = 1;
my $r = 0;

my $filename = "r.txt";

srand(time());

while($increment <= $N){
	$r = rand($N);
	if( $numbers{$r} ){
		#nothing
	}else{
		$numbers{$r} = $increment;
		$increment++;
	}
}

open ORDER, ">$filename" or die "crap";
print ORDER "$size\t$size\n";
my @ordKeys = sort by_count keys %numbers;
foreach my $i (@ordKeys){
	my $c = $numbers{$i} - 1;
	print ORDER "$c\n";
}
close ORDER;

system("chmod 644 $filename");

sub by_count {
	return $a <=> $b;
}
sub randNum {
	return int ( rand( $_[1] - $_[0] + 1 ) ) + $_[0];
}
