import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;

class fileop
{
	void writefile(int src[], String fname, int powr) throws Exception {

		OutputStream fout = new FileOutputStream(fname);
		int a, siz = src.length;
		int tempo, dempo, jumpo;
		boolean flag = false;

		a = Math.abs(src[siz - 1]);
		
		if (a > 1000000000)
			jumpo = 1000000000;
		else if ( a > 100000000)
			jumpo = 100000000;
		else if ( a > 10000000)
			jumpo = 10000000;
	        else if ( a > 1000000)
			jumpo = 1000000;
		else if (a > 100000)
			jumpo = 100000;
		else if (a > 10000)
			jumpo = 10000;
		else if (a > 1000)
			jumpo = 1000;
		else if ( a > 100)
			jumpo = 100;
		else if ( a > 10)
			jumpo = 10;
		else 
			jumpo = 1;

		for (int i = 0; i < siz; i++) {
			a = src[i];
			dempo = jumpo;
			if (a < 0) {
				fout.write('-');
				a = Math.abs(a);
			}
			if (a == 0)
				fout.write(a + 48);
			else {
				while(dempo != 0) {
					if (a > dempo)
						flag = true;
					if (flag) {
						tempo = a / dempo;
						a %= dempo;
						fout.write(tempo + 48);	
					}
					dempo /= 10;
				}
				flag  = false;
			}
			fout.write(10);
		}
	}

	void readfile(int src[], String fname) throws Exception {
		InputStream fin = new FileInputStream(fname);
		int a = 0, temp = 100, ind = 0;
		do {
			temp = fin.read();
			if (temp != '\n') 
				a = 10*a + temp - 48;
			else
			{
				src[ind] = a;
				ind++;
				a = 0;
			}
		} while (temp != -1);	
		fin.close();
	}
	
	int size(String fname) throws Exception {
		int temp, siz = 0;
		InputStream fin = new FileInputStream(fname);
		do {
			temp = fin.read();
			if (temp == '\n')
				siz++;
		} while (temp != -1);	
		fin.close();
		return siz;
	}
		
}

public class sortmst
{
   public static void main(String args[]) throws Exception
   {
      int job_id = 1;
      int task_id = 100;

	fileop fp = new fileop();
      int main_pno;
      String main_pt;
      int pt_no;
      String  pt_name;
	String fname1, fname2, fname3;

	DataInputStream datain = new DataInputStream(System.in);

	System.out.println("Enter The name of first file : ");
	fname1 = datain.readLine();
	System.out.println("Enter The name of Second file : ");
	fname2 = datain.readLine();
	System.out.println("Enter The name of Destination file : ");
	fname3 = datain.readLine();

      int i,  status; 
      int a_num, b_num;
	a_num = fp.size(fname1);
	b_num = fp.size(fname2);
	int nl;
	int a[] = new int[a_num];
	int b[] = new int[b_num];
	int c[] = new int[a_num + b_num];
		

      JMPF def_obj = new JMPF(job_id, task_id);
      System.out.println("The hostname is " + def_obj.hostname);
      System.out.println("The host id is " + def_obj.host_id);
		
// reads the first set of data and stores in the array a.
	fp.readfile(a, fname1);

// reads the second set of data and stores in the array b.
	fp.readfile(b, fname2);

// creation of the main port.
      main_pt = "main";
      main_pno = def_obj.CreatePort(main_pt);


// send the first set to the worker processes.
       pt_no = def_obj.LocatePort(String.valueOf(0), true);
       String string = String.valueOf(a_num) + " ";
	    for (i = 0; i < a_num; i++)
       	string = string + String.valueOf(a[i]) + " ";
       status = def_obj.Send(pt_no, string);

// send the second set to the worker processes.
       pt_no = def_obj.LocatePort(String.valueOf(1), true);
       string = String.valueOf(b_num) + " ";
	    for (i = 0; i < b_num; i++)
       	string = string + String.valueOf(b[i]) + " ";
       status = def_obj.Send(pt_no, string);

// receive the results from the first worker and store in the matrix a.
      Integer S = new Integer(2);
      synchronized(S)
      {
        	String str = def_obj.ReceiveString(main_pno);
        	StringTokenizer st = new StringTokenizer(str);
         for (i = 0; i < a_num; i++)
            a[i] = Integer.parseInt(st.nextToken());
      }

// receive the results from the second worker and store in the matrix b.
      synchronized(S)
		{
        	String str = def_obj.ReceiveString(main_pno);
        	StringTokenizer st = new StringTokenizer(str);
         for (i = 0; i < b_num; i++)
            b[i] = Integer.parseInt(st.nextToken());
      }

// merge sort the two arrays a & b and store them in c.
			int j = 0, k = 0; 
			int total_num = a_num + b_num;
			for (i = 0; j < a_num && k < b_num; i++)
			{
				if (a[j] < b[k])
				{
					c[i] = a[j];
					j++;
				}
				else
				{
					c[i] = b[k];
					k++;
				}
			}
			if (j == a_num)
				while (k < b_num)
					c[i++] = b[k++];

			else 
				while (j < a_num)
					c[i++] = a[j++];


	fp.writefile(c, fname3, 9);

      def_obj.ClosePorts(); 
   }
}

