import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.Scanner;
class ProofRead
{	
	
	static Formatter output;
	static Formatter output1;
	static Formatter output2;//for writing the vertical frame tiles
	static Formatter output3;//for writing the horiz6ontal frame tiles
	static Formatter output4;//for writing the seed tile
	
	static Formatter output5;
	static Formatter output6;
	static Formatter output7;
	
	static Formatter output8;
	
	static Scanner input1;
	static Scanner input2;
	static Scanner input3;
	static Scanner input4;
	static Scanner input5;//for reading frame tiles' glues
	
	static Scanner input6;//for proofreading verticalframe tiles' glues
	static Scanner input7;//for proofreading horizontal frame tiles' glues
	static Scanner input8;//for proofreading seed frame tiles' glues
	
	static Scanner input9;//for finding total glues in computational tiles before any error correction;
	
	static Scanner input10;
	
	static Scanner input11;
	static Scanner input12;
	static Scanner input13;
	
	
	public static void main(String args[])
	{	int m;//for m*m proofread the value of m
	
		System.out.println("We can give you the tileset for proofread of m*m give us the value of m \n allowable values of n are 2,4,16 and so on ");
		Scanner input = new Scanner(System.in);
		m = input.nextInt();
		
		int strength[] = new int[2000];//array for glue strength 
		
		int y = 0, big = 0;
		
		int glue_strength_1 =0, glue_strength_2 = 0;
		
		try
		{
			input1 = new Scanner(new File("glues2.txt"));//all the initial frame & seed tiles' glue
			input2 = new Scanner(new File("glues.txt"));//all the initial computational tiles(non frame and non seed)
			input5 = new Scanner(new File("glues2.txt"));
			input9 = new Scanner(new File("glues.txt"));
									
			
			output2 = new Formatter("vertical_frame_tiles.txt");
			output3 = new Formatter("horizontal_frame_tiles.txt");
			output4 = new Formatter("seed_tile.txt");
			
			output5 = new Formatter("seed_only.txt");
			output6 = new Formatter("frame_only.txt");
			output7 = new Formatter("comp_only.txt");
			
			
			int n,e,s,w;
			//to find the total glues in computational tiles before any error correction;
			int x1=0,big2=0;
			
			while(input9.hasNext())
			 	{
			 		x1 = input9.nextInt();
			 		if(big2<x1)
			 		big2=x1;//big2 is highest no. of the inner tile without any manipulation
			 		
			 	}
			//System.out.printf("%d\n",big2);
			while(y < big2)
			{
				strength[y] = 1;
				y++;
			}//strength of the inner tiles
			
			while(input5.hasNext())
			{
				n=input5.nextInt();
				e=input5.nextInt();
				s=input5.nextInt();
				w=input5.nextInt();
				
				if(e==0 && s!=0)
					output2.format("%d %d %d %d\n",n,e,s,w);
				if(e==0 && s==0)
					output4.format("%d %d %d %d\n",n,e,s,w);
				if(e!=0 && s==0)
					output3.format("%d %d %d %d\n",n,e,s,w);
			
			}
			output2.close();
			output3.close();
			output4.close();
			
			output = new Formatter("glue_proof_read.txt");
			int x;
			big=0;
			while(input1.hasNext())
			 	{
			 		x = input1.nextInt();
			 		if(big<x)
			 		big=x;
			 		
			 	}
			 	int big3 = big;//big3 is highest no. before any manipulation in frames as well as inner tiles
			 	//System.out.printf("\n %d\n",big);
			 	while(y < big3)
			 	{
			 		strength[y] = 2;
			 		y++;
			 	}//for strength of the initial frame tiles
			 	
			int a,b,c,d;
			int h[]=new int[(m*m)+m];//for specifying the inner horizontal glues while Proofreading
			int v[]=new int[(m*m)+m];//for specifying the inner vertical glues while Proofreading
			int i,j;
			
			while(input2.hasNext())
			{
				a = input2.nextInt();
				b = input2.nextInt();
				c = input2.nextInt();
				d = input2.nextInt();
				
				for(i=0;i<(m*m);i = i+(m+1))//for filling the side glues of a broken computational tile
				{
					v[i]=b;
					v[i+m]=d;
					h[i]=c;
					h[i+m]=a;
				}
				
					/*for(i=0;i<(m*m);i = i+(m+1))
				{
					v[i]=b;
					v[i+m]=d;
					h[i]=c;
					h[i+m]=a;
				}*/
				
				for(j=1;j<=m;j++)//for filling the horizontal glues of inner tiles of a broken tile while Proofreading
				{
					for(i=1+((m+1)*(j-1));i<((m+1)*j)-1;i++)
					{
						h[i]=++big;
					}	
				}
				for(j=1;j<=m;j++)//for filling the vertical glues of inner tiles of a broken tile while Proofreading
				{
					for(i=1+((m+1)*(j-1));i<((m+1)*j)-1;i++)
					{
						v[i]=++big;
					}
				}
				
				int z1,z2,z3,z4;
				
				for(j=0;j<m;j++)//for writting the computational tiles glues after proofreading in a table in file glues_proof_read.txt
				{
					for(i=0,z1=j*((m+1))+1,z2=j,z3=j*(m+1),z4=j+1;i<m;i++,z1++,z2=z2+(m+1),z3++,z4=z4+(m+1))
					{
						output.format("%d %d %d %d\n",h[z1],v[z2],h[z3],v[z4]);
						output7.format("%d %d %d %d\n",h[z1],v[z2],h[z3],v[z4]);
						
					}
				}
			}
			//glue_strength_1 = big - (big3 - big2);
			//System.out.printf("\n%d\n",big);
			while(y < big)//strength of all the computational tiles which have been broken
			{
				strength[y] = 1;
				y++;
			}
			
			
			
		//vertical frame tiles being broken for proofread
		
		input6 = new Scanner(new File("vertical_frame_tiles.txt"));
		a= b= c= d= 0;
		int hr[] = new int[m+1];//m+1
		int vr[] = new int[2*m]; //2*m		
		while(input6.hasNext())
		{
			a = input6.nextInt();
			b = input6.nextInt();
			c = input6.nextInt();
			d = input6.nextInt();
			
			for(i = 0; i<= m-1; i++) //i <= m-1
				vr[i] = b;
			
			for(i = m; i<= 2*m - 1; i++) //i = n; i <= 2*n - 1
				vr[i] = d;
			
				hr[0] = c;
			
				hr[m] = a; //hr[n]
			
			for(i = 1; i <= m-1; i++ ) //	i <=n-1		
				hr[i] = ++big;
			
			for(i = 0;i < m; i++ ) //i<n
			{	
				output.format("%d %d %d %d\n",hr[i+1], vr[i], hr[i], vr[i+m]); //vr[i+n]
				output6.format("%d %d %d %d\n",hr[i+1], vr[i], hr[i], vr[i+m]);
			}
		}
			
			//horizontal frame tiles being broken for proofread
		
		input7 = new Scanner(new File("horizontal_frame_tiles.txt"));
		a= b= c= d= 0;
		int vr1[] = new int[m+1];//n+1
		int hr1[] = new int[2*m]; //2*n		
		while(input7.hasNext())
		{
			a = input7.nextInt();
			b = input7.nextInt();
			c = input7.nextInt();
			d = input7.nextInt();
			
			for(i = 0; i<= m-1; i++) //i <= n-1
				hr1[i] = c;
			
			for(i = m; i<= 2*m - 1; i++) //i = n; i <= 2*n - 1
				hr1[i] = a;
			
				vr1[0] = b;
			
				vr1[m] = d; //vr1[n]
			
			for(i = 1; i <= m-1; i++ ) //	i <=n-1		
				vr1[i] = ++big;
			
			for(i = 0;i < m; i++ ) //i<n
			{
				output.format("%d %d %d %d\n",hr1[i+m], vr1[i], hr1[i], vr1[i+1]); //hr1[i+n]
				output6.format("%d %d %d %d\n",hr1[i+m], vr1[i], hr1[i], vr1[i+1]); //hr1[i+n]
			}
		}
			
		//for seed tile
		input8 = new Scanner(new File("seed_tile.txt"));
		input10 = new Scanner(new File("seed_tile.txt"));
		
		input11 = new Scanner(new File("seed_only.txt"));
		input12 = new Scanner(new File("frame_only.txt"));
		input13 = new Scanner(new File("comp_only.txt"));
		
		output.format("%d %d %d %d\n", input8.nextInt(),input8.nextInt(),input8.nextInt(),input8.nextInt());	
		output5.format("%d %d %d %d\n", input10.nextInt(),input10.nextInt(),input10.nextInt(),input10.nextInt());	
					
		output.close();	
		output5.close();
		output6.close();
		output7.close();
		//glue_strength_2 = big - glue_strength_1;
		//System.out.printf("%d\n",big);
		
		//writing tiles in the correct order------------------------------------------------------
		
		output8 = new Formatter("gpf_read.txt");
		
		while(input11.hasNext())
		{
			output8.format("%d %d %d %d\n",input11.nextInt(),input11.nextInt(),input11.nextInt(),input11.nextInt());
		}
		
		while(input12.hasNext())
		{
			output8.format("%d %d %d %d\n",input12.nextInt(),input12.nextInt(),input12.nextInt(),input12.nextInt());
		}
		
		while(input13.hasNext())
		{
			output8.format("%d %d %d %d\n",input13.nextInt(),input13.nextInt(),input13.nextInt(),input13.nextInt());
		}
		
		output8.close();
		
		while(y < big)
		{
			strength[y] = 2;
			y++;
		}
		}
		
		catch ( SecurityException securityException )
      {
         System.err.println(
            "You do not have write access to this file." );
         System.exit( 1 );
      } // end catch
      catch ( FileNotFoundException filesNotFoundException )
      {
         System.err.println( "Error creating file." );
         System.exit( 1 );
      } // end catch
      catch ( FormatterClosedException formatterClosedException )
         {
            System.err.println( "Error writing to file." );
            return;
         } // end catch
         catch ( NoSuchElementException elementException )
        {
            System.err.println( "Invalid input. Please try again." );
            input6.nextLine(); // discard input so user can try again
         } // end catch
		
		
		//converting the glue table into a .tiles file
		
		try
		 {
		 	input3 = new Scanner(new File("gpf_read.txt"));
		 	input4 = new Scanner(new File("gpf_read.txt"));
		 	int counter,big1,a1;
			counter = 0;
			big1=0;	
		 	while(input3.hasNext())
		 	{
		 		a1 = input3.nextInt();
		 		if(big1<a1)
		 		big1=a1;
		 		++counter;
		 	}
		 	int num_tile_types = counter/4;
		 	System.out.printf("big ele is %d and no. o tiles is %d\n",big1,num_tile_types);
		 
		 output1 = new Formatter("proof_read.tiles");
		 output1.format("tile edges matches {{N E S W}*}\n");
		 output1.format("num tile types=%d\n",num_tile_types);
		 output1.format("num binding types=%d\ntile edges={\n",big1);
		 int p,q,r,s;
		 while(input4.hasNext())
		 {
		 	p=input4.nextInt();
		 	q=input4.nextInt();
		 	r=input4.nextInt();
		 	s=input4.nextInt();
		 	output1.format("{%d\t%d\t%d\t%d}\n",p,q,r,s);
		 }
		 output1.format("}\n");
		 output1.format("binding strengths=\n{");
			
			for(y = 0; y < big; y++)
			{
				output1.format("%d\t",strength[y]);
			}
			
			/*for(int d1=0; d1< glue_strength_2; d1++)
			{
				output1.format("2\t");
			}*/	 
			output1.format("}");
		 output1.close();
		 }
		 catch ( FileNotFoundException fileNotFoundException )
      	 {
          	System.err.println( "Error opening file." );
         	System.exit( 1 );
      	 } // end catch
		
		
	}		
}