#include <bits/stdc++.h>
using namespace std;
int a[1000],b[1000];

 bool search_num(int c[], int n, int x)
 {
     for(int i=0;i<n;i++)
     {
         if(c[i]==x) return true;

     }
     return false;
 }
int main()
{
   int n,m,j=0,x=0,y=0;
   cin>>n;
   for(int i=0;i<n;i++) cin>>a[i];
   sort(a,a+n);
   cin>>m;
   for(int i=0;i<m;i++) cin>>b[i];
   sort(b,b+n);
   for(int j=0;j<n;j++)
   for(int i=0;i<m;i++)
   {
       if(!(search_num(b,m,a[j]+b[i]))&&!(search_num(a,n,a[j]+b[i])))
       {
          x=a[j];
          y=b[i];
          goto end1;
       }
   }
  end1: cout<<x<<" "<<y<<endl;
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m,i,j;
    int A[101],B[101];
          cin>>n;
    for( i=0; i<n; i++){

        cin>>A[i];
            sort(A,A+n);}
      cin>>m;
    for( j=0; j<m; j++){

        cin>>B[j];
            sort(B,B+m);
    }

    cout<<A[n-1]<<" "<<B[m-1]<<endl;
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m,i,j;
    int A[101],B[101];
          cin>>n;
    for( i=0; i<n; i++){

        cin>>A[i];}
      cin>>m;
    for( j=0; j<m; j++){

        cin>>B[j];
    }
    sort(A,A+n);
    sort(B,B+m);
    cout<<A[n-1]<<" "<<B[m-1]<<endl;
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m,i,j;
    int A[105],B[105];
          cin>>n;
    for( i=0; i<n; i++){

        cin>>A[i];}
      cin>>m;
    for( j=0; j<m; j++){

        cin>>B[j];
    }
    sort(A,A+n);
    sort(B,B+m);
    cout<<A[n-1]<<" "<<B[m-1]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,m,n,a[104],b[104];
for(cin>>n;i<n;)
cin>>a[i++];
for(cin>>m;j<m;)
cin>>b[j++];
sort(a,a+n);
sort(b,b+m);
cout<<a[n-1]<<" "<<b[m-1];
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m;

    int A[101],B[201];
    int i,j;

    cin>>n;

    for( i=1; i<=n; i++)

        cin>>A[i];

    cin>>m;
    for( j=1; j<=m; j++)

        cin>>B[j];

    sort(A,A+n);
    sort(B,B+m);

    cout<<A[n-1]<<" "<<B[m-1]<<endl;

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m;
    int A[101],B[101];
    int i,j;
    cin>>n;
    for( i=1; i<=n; i++)
        cin>>A[i];
    cin>>m;
    for( j=1; j<=m; j++)
        cin>>B[j];
    sort(A,A+n);
    sort(B,B+m);
    cout<<A[n-1]<<" "<<B[m-1]<<endl;
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m;

    int A[101],B[201];
    int i,j;

    cin>>n;

    for( i=1; i<=n; i++)

    {
        cin>>A[i];

    }
     sort(A,A+n);

    cin>>m;
    for( j=1; j<=m; j++)
    {
        cin>>B[j];

    }
          sort(B,B+m);

    cout<<n-1<<" "<<j-1<<endl;

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>

using namespace std;
int main()
{
    int n,m;

    int A[101],B[201];
    int i,j;

    cin>>n;

    for( i=1; i<=n; i++)

    {
        cin>>A[i];

    }
     sort(A,A+n);

    cin>>m;
    for( j=1; j<=m; j++)
    {
        cin>>B[j];

    }
          sort(B,B+m);

    cout<<i-1<<" "<<j-1<<endl;

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,i,a=0,b=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		int k;
		cin>>k;
		if(k>a)
		a=k;
    }
	cin>>n;
	for(i=0;i<n;i++)
	{
		int k;
		cin>>k;
		if(k>b)
		b=k;
	}
	cout<<a<<" "<<b;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,m,n,a[105],b[105];
for(cin>>n;i<n;)
cin>>a[i++];
for(cin>>m;j<m;)
cin>>b[j++];
sort(a,a+n);
sort(b,b+m);
cout<<a[n-1]<<" "<<b[m-1];
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n, m, flag=0;
	cin >> n;
	int arr1[n];
	for(int i=0;i<n;i++)
	{
		cin >> arr1[i];
	}
	cin >> m;
	int arr2[m];
	for(int i=0;i<m;i++)
	{
		cin >> arr2[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(arr1[i]!=arr2[j])
			{
				cout << arr1[i] << " " << arr2[j] << endl;
				flag=1;
				break;
			}
		}
		if(flag==1)
		{
			break;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n, m;
	cin >> n;
	int arr1[n];
	for(int i=0;i<n;i++)
	{
		cin >> arr1[i];
	}
	cin >> m;
	int arr2[m];
	for(int i=0;i<m;i++)
	{
		cin >> arr2[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(arr1[i]!=arr2[j])
			{
				cout << arr1[i] << " " << arr2[j] << endl;
				int flag=1;
				break;
			}
			if(flag==1)
			{
				break;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n, m, flag=0;
	cin >> n;
	int arr1[n];
	for(int i=0;i<n;i++)
	{
		cin >> arr1[i];
	}
	cin >> m;
	int arr2[m];
	for(int i=0;i<m;i++)
	{
		cin >> arr2[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(arr1[i]!=arr2[j])
			{
				cout << arr1[i] << " " << arr2[j] << endl;
				flag=1;
				break;
			}
			if(flag==1)
			{
				break;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n, m;
	cin >> n;
	int arr1[n];
	for(int i=0;i<n;i++)
	{
		cin >> arr1[i];
	}
	cin >> m;
	int arr2[m];
	for(int i=0;i<m;i++)
	{
		cin >> arr2[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(arr1[i]!=arr2[j])
			{
				cout << arr1[i] << " " << arr2[j] << endl;
				break;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

bool ispresent(int x, int a1[], int a2[], int n, int m)
{
    bool f = false;
    for(int i=0;i<n;i++)
    {
        if(x == a1[i]){f = true;break;}
    }
    for(int i=0;i<m;i++)
    {
        if(x == a2[i]){f = true;break;}
    }
    return f;
}

int main()
{
    int n, m, u, v;
    cin >> n;
    int a1[n];
    for(int i=0;i<n;i++){cin >> a1[i];}
    cin >> m;
    int a2[m];
    for(int i=0;i<m;i++){cin >> a2[i];}

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!ispresent(a1[i]+a2[j], a1, a2, n, m)){u = a1[i];v = a2[j];break;}
        }
    }
    cout << u << " " << v;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,fla1=0,fla2=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    cout<<a[n]<<" "<<b[m]<<endl;
    /*int h=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        { fla1=0,fla2=0;
            int x=a[i]+b[j];
            int l=i,r=j;
            while(x>a[l]&&l<=n)l++;
            if(x==a[l])fla1=1;
            while(x>b[r]&&r<=m)r++;
            if(x==b[r])fla2=1;++h;
            if(fla1||fla2)continue;
            else {cout<<a[i]<<" "<<b[j]<<endl;return 0;

            }
        }
    }
    cout<<h<<endl;*/
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,fla1=0,fla2=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        { fla1=0,fla2=0;
            int x=a[i]+b[j];
            int l=i,r=j;
            while(x>a[l]&&l<=n)l++;
            if(x==a[l])fla1=1;
            while(x>b[r]&&r<=m)r++;
            if(x==b[r])fla2=1;
            if(fla1||fla2)continue;
            else {cout<<a[i]<<" "<<b[j]<<endl;return 0;}
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,m,fla=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int y=0;
            int x=a[i]+b[j];
            //cout<<a[i]<<" "<<b[j]<<" "<<x<<endl;
            for(int k=1;k<=max(n,m);k++)
            {
                if((x==a[k]&&k<=n)||(x==b[k]&&k<=m)){fla=1;break;}
                else y++;
            }
            if(!fla&&y==max(n,m)){cout<<a[i]<<" "<<b[j]<<endl;return 0;}
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,m,fla=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int y=0;
            int x=a[i]+b[j];
            for(int k=1;k<=n;k++)
            {
                if(x==a[k]){fla=1;break;}
            }
            if(fla)continue;
            else
            {
                for(int r=1;r<=m;r++)
                {
                    if(x==b[r])break;
                    else y++;
                }
                if(y==m){cout<<a[i]<<" "<<b[j]<<endl;return 0;}
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,m,fla=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int y=0;
            int x=a[i]+b[j];
            for(int k=1;k<=n;k++)
            {
                if(x==a[k]){fla=1;break;}
            }
            if(fla)continue;
            else
            {   fla=0;
                for(int r=1;r<=m;r++)
                {
                    if(x==b[r]){fla=1;break;}
                }
                if(!fla){cout<<a[i]<<" "<<b[j]<<endl;return 0;}
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,m,fla=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int y=0;
            int x=a[i]+b[j];
            for(int k=1;k<=n;k++)
            {
                if(x==a[k]){fla=1;break;}
            }
            if(fla)continue;
            else
            {   fla=0;
                for(int r=1;r<=m;r++)
                {
                    if(x==b[r]){break;}
                    else y++;
                }
                if(y==m){cout<<a[i]<<" "<<b[j]<<endl;return 0;}
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[200],b[200];
int main()
{
    int n,fla1=0,fla2=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
    }
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        { fla1=0,fla2=0;
            int x=a[i]+b[j];
            int l=i,r=j;
            while(x>a[l])l++;
            if(x==a[l])fla1=1;
            while(x>b[r])r++;
            if(x==b[r])fla2=1;
            if(fla1||fla2)continue;
            else {cout<<a[i]<<" "<<b[j]<<endl;return 0;}
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

    unordered_map<int,int> m;
    int n,mp,A[101],B[101];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>A[i];

    cin>>mp;
    for(int j=0;j<mp;j++)
        cin>>B[j];

    for(int i=0;i<n;i++)
        m[A[i]]=1;

    for(int i=0;i<mp;i++)
        m[B[i]]=1;

    bool found=false;
    for(int i=0;i<n;i++)
    {
        if(found)
            break;
        for(int j=0;j<mp;j++)
        {
            if(m[A[i]+A[j]]==1)
            {
                cout<<A[i]<<" "<<B[j]<<endl;
                found=true;
                break;
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

    unordered_map<int,int> m;
    int n,m,A[101],B[101];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>A[i];

    cin>>m;
    for(int j=0;j<m;j++)
        cin>>B[j];

    for(int i=0;i<n;i++)
        m[A[i]]=1;

    for(int i=0;i<m;i++)
        m[B[i]]=1;

    bool found=false;
    for(int i=0;i<n;i++)
    {
        if(found)
            break;
        for(int j=0;j<m;j++)
        {
            if(m[A[i]+A[j]]==1)
            {
                cout<<A[i]<<" "<<B[j]<<endl;
                found=true;
                break;
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

    unordered_map<int,int> m;
    int n,mp,A[101],B[101];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>A[i];

    cin>>mp;
    for(int j=0;j<mp;j++)
        cin>>B[j];

    for(int i=0;i<n;i++)
        m[A[i]]=1;

    for(int i=0;i<mp;i++)
        m[B[i]]=1;

    bool found=false;
    for(int i=0;i<n;i++)
    {
        if(found)
            break;
        for(int j=0;j<mp;j++)
        {
            if(m[A[i]+B[j]]==0)
            {
                cout<<A[i]<<" "<<B[j]<<endl;
                found=true;
                break;
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

    unordered_map<int,int> m;
    int n,mp,A[101],B[101];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>A[i];

    cin>>mp;
    for(int j=0;j<mp;j++)
        cin>>B[j];

    for(int i=0;i<n;i++)
        m[A[i]]=1;

    for(int i=0;i<mp;i++)
        m[B[i]]=1;

    bool found=false;
    for(int i=0;i<n;i++)
    {
        if(found)
            break;
        for(int j=0;j<mp;j++)
        {
            if(m[A[i]+B[j]]==1)
            {
                cout<<A[i]<<B[j]<<endl;
                found=true;
                break;
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

    unordered_map<int,int> m;
    int n,mp,A[101],B[101];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>A[i];

    cin>>mp;
    for(int j=0;j<mp;j++)
        cin>>B[j];

    for(int i=0;i<n;i++)
        m[A[i]]=1;

    for(int i=0;i<mp;i++)
        m[B[i]]=1;

    bool found=false;
    for(int i=0;i<n;i++)
    {
        if(found)
            break;
        for(int j=0;j<mp;j++)
        {
            if(m[A[i]+B[j]]==1)
            {
                cout<<A[i]<<" "<<B[j]<<endl;
                found=true;
                break;
            }
        }
    }

    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main(){
	int n,m;
	scanf("%d",&n);
	int ar[n];
	for(int i=0;i<n;i++){
		scanf("%d",&ar[i]);
	}
	sort(ar,ar+n);
	scanf("%d",&m);
	int br[m];
	for(int i=0;i<n;i++){
		scanf("%d",&br[i]);
	}
	sort(br,br+n);
	printf("%d %d\n",ar[n-1],br[n-1]);
	return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main(){
	int n,m;
	scanf("%d",&n);
	int ar[n];
	for(int i=0;i<n;i++){
		scanf("%d",&ar[i]);
	}
	sort(ar,ar+n);
	scanf("%d",&m);
	int br[m];
	for(int i=0;i<m;i++){
		scanf("%d",&br[i]);
	}
	sort(br,br+m);
	printf("%d %d\n",ar[n-1],br[m-1]);
	return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main(){
	int n,m;
	scanf("%d",&n);
	int ar[n];
	for(int i=0;i<n;i++){
		scanf("%d",&ar[i]);
	}
	sort(ar,ar+n);
	scanf("%d",&m);
	int br[m];
	for(int i=0;i<n;i++){
		scanf("%d",&br[i]);
	}
	sort(br,br+n);
	printf("%d %d\n",ar[n-1],br[m-1]);
	return 0;
}


#include<iostream>

using namespace std;

int main(){
	int n,m;
	scanf("%d",&n);
	int ar[n];
	for(int i=0;i<n;i++){
		scanf("%d",&ar[i]);
	}
	sort(ar,ar+n);
	scanf("%d",&m);
	int br[m];
	for(int i=0;i<n;i++){
		scanf("%d",&br[i]);
	}
	sort(br,br+n);
	printf("%d %d\n",ar[n-1],br[n-1]);
	return 0;
}


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0; i < n; ++i)
   {
      for (int j = 0; j <m ; ++j)
      {

       if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
       {
          std::cout<<A[i]<<" "<<B[j]<<std::endl;
          exit(1);
       }
      }
   }
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0,int j=m; i < n ,j>0; ++i,++j)
   {
     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
        break;
     }
   }
   return 0;
 }


#include <iostream>
#include <cstdlib>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   int i=0,j=0;
       while(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
       {
          i=ternary_search(B,0,n-1, A[i]+B[j])
          j=ternary_search(A,0,n-1, A[i]+B[j])
       }
       std::cout<<A[i]<<" "<<B[j]<<std::endl;
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0,j=m-1; i < n ,j>=0; ++i,++j)
   {
     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
        break;
     }
   }
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0,j=m; i < n ,j>0; ++i,++j)
   {
     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
        break;
     }
   }
   return 0;
 }


#include <iostream>
#include <cstdlib>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,m-1);
   int i=0,j=0;
       while(ternary_search(B,0,m-1, A[i]+B[j])!=-1 || ternary_search(A,0,n-1, A[i]+B[j])!=-1)
       {
        if(ternary_search(B,0,m-1, A[i]+B[j])!=-1 ) j=ternary_search(B,0,m-1, A[i]+B[j]);
        if(ternary_search(A,0,n-1, A[i]+B[j])!=-1)  i=ternary_search(A,0,n-1, A[i]+B[j]);

       }
       std::cout<<A[i]<<" "<<B[j]<<std::endl;
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(mid2+1,r,x);
        else
            return ternary_search(mid1+1,mid2-1,x);

    }
    return -1;

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0,int j=m; i < n ,j>0; ++i,++j)
   {
     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
        break;
     }
   }
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
}    return -1;

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0,int j=m; i < n ,j>0; ++i,++j)
   {
     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
        break;
     }
   }
   return 0;
 }


#include <iostream>
#include <cstdlib>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   int i=0,j=0;
       while(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
       {
          i=rand()%n;
          j=rand()%m;
       }
       std::cout<<A[i]<<" "<<B[j]<<std::endl;
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0; i < n; ++i)
   {
    for (int j = 0; j <m ; ++j)
    {

     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<" "<<B[j]<<std::endl;
        exit(1);
     }
   }
   }
   return 0;
 }


#include <iostream>
#include <cstdlib>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   int i=0,j=0;
       while(ternary_search(B,0,n-1, A[i]+B[j])!=-1 || ternary_search(A,0,n-1, A[i]+B[j])!=-1)
       {
        if(ternary_search(B,0,n-1, A[i]+B[j])!=-1 )i=ternary_search(B,0,n-1, A[i]+B[j]);
        if(ternary_search(A,0,n-1, A[i]+B[j])!=-1)  j=ternary_search(A,0,n-1, A[i]+B[j]);

       }
       std::cout<<A[i]<<" "<<B[j]<<std::endl;
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0; i < n; ++i)
   {
    for (int j = 0; j <m ; ++j)
    {

     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
        break;
     }
   }
   }
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(mid2+1,r,x);
        else
            return ternary_search(mid1+1,mid2-1,x);

    }
    return -1;

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0,int j=m; i < n ,j>0; ++i,++j)
   {
     if(ternary_search(B,0,n-1, A[i]+B[j])==-1 && ternary_search(A,0,n-1, A[i]+B[j])==-1)
     {
        std::cout<<A[i]<<B[j]<<std::endl;
     }
   }
   return 0;
 }


#include <iostream>
void swap(int &a,int &b)
{
  int temp =a;
  a=b;
  b=temp;
}
int partition ( int A[],int start ,int end) {
    int i = start + 1;
    int piv = A[start] ;            //make the first element as pivot element.
    for(int j =start + 1; j <= end ; j++ )  {
    /*rearrange the array by putting elements which are less than pivot
       on one side and which are greater that on other. */

          if ( A[ j ] < piv) {
                 swap (A[ i ],A [ j ]);
            i += 1;
        }
   }
   swap ( A[ start ] ,A[ i-1 ] ) ;  //put the pivot element in its proper place.
   return i-1;                      //return the position of the pivot
}
void quick_sort ( int A[ ] ,int start , int end ) {
   if( start < end ) {
        //stores the position of pivot element
         int piv_pos = partition (A,start , end ) ;
         quick_sort (A,start , piv_pos -1);    //sorts the left side of pivot.
         quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot.
   }
}
int ternary_search(int ar[],int l,int r, int x)
{
    if(r>=l)
    {
        int mid1 = l + (r-l)/3;
        int mid2 = r -  (r-l)/3;
        if(ar[mid1] == x)
            return mid1;
        if(ar[mid2] == x)
            return mid2;
        if(x<ar[mid1])
            return ternary_search(ar,l,mid1-1,x);
        else if(x>ar[mid2])
            return ternary_search(ar,mid2+1,r,x);
        else
            return ternary_search(ar,mid1+1,mid2-1,x);

    }
    return -1;
}

int main()
 {
   int n,m;
   std::cin>>n;
   int A[n];
   for (int i = 0; i < n; ++i)
   {
     std::cin>>A[i];
   }
   std::cin>>m;
   int B[m];
   for (int i = 0; i < m; ++i)
   {
     std::cin>>B[i];
   }
   quick_sort(A,0,n-1);
   quick_sort(B,0,n-1);
   for (int i = 0; i < n && i<m; ++i)
   {
       if(ternary_search(B,0,n-1, A[i]+B[i])==-1 && ternary_search(A,0,n-1, A[i]+B[i])==-1)
       {
          std::cout<<A[i]<<" "<<B[i]<<std::endl;
          exit(1);
       }
   }
   return 0;
 }


#include<iostream>
using namespace std;
void nhap(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
}
int ss(int a[],int n,int b)
{
	for(int i=0;i<n;i++)
	{
		if(a[i]==b)
			return 0;
	}
	return 1;
}
void kt(int a[],int b[],int n, int m)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int	c=a[i]+b[j];
			if(ss(a,n,c)==1&&ss(b,m,c)==1)
			{
				cout<<a[i]<<" "<<b[j]<<" ";
				return;
			}

		}
	}
}
int main()
{
	int n, m,a[100],b[100];
	cin >> n;
	nhap(a,n);
	cin >>m;
	nhap(b,m);
	kt(a,b,n,m);
	return 0;

}


#include<iostream>
using namespace std;
int nhap(int a[],int n)
{
	int max=0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		max=max>=a[i]?max:a[i];
	}
	return max;
}
int ss(int a[],int n,int b)
{
	for(int i=0;i<n;i++)
	{
		if(a[i]==b)
			return 0;
	}
	return 1;
}
void kt(int a[],int b[],int n, int m)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int	c=a[i]+b[j];
			if(ss(a,n,c)==1&&ss(b,m,c)==1)
			{
				cout<<a[i]<<" "<<b[j]<<" ";
				return;
			}

		}
	}
}
int main()
{
	int n, m,a[100],b[100];
	cin >> n;
	int max1=nhap(a,n);
	cin >>m;
	int max2=nhap(b,m);
	//kt(a,b,n,m);
	cout<< max1+" "+max2;
	return 0;
}


#include<iostream>
using namespace std;
int nhap(int a[],int n)
{
	int max=0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		max=max>=a[i]?max:a[i];
	}
	return max;
}
int ss(int a[],int n,int b)
{
	for(int i=0;i<n;i++)
	{
		if(a[i]==b)
			return 0;
	}
	return 1;
}
void kt(int a[],int b[],int n, int m)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int	c=a[i]+b[j];
			if(ss(a,n,c)==1&&ss(b,m,c)==1)
			{
				cout<<a[i]<<" "<<b[j]<<" ";
				return;
			}

		}
	}
}
int main()
{
	int n, m,a[100],b[100];
	cin >> n;
	int max1=nhap(a,n);
	cin >>m;
	int max2=nhap(b,m);
	//kt(a,b,n,m);
	return max1+max2;

}


#include<iostream>
using namespace std;
void nhap(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
}
int ss(int a[],int n,int b)
{
	for(int i=0;i<n;i++)
	{
		if(a[i]==b)
			return 0;
	}
	return 1;
}
void kt(int a[],int b[],int n, int m)
{
	for(int i=0;i<n;i++)
	{	int c=0;
		for(int j=0;j<m;j++)
		{
			c=a[i]+b[j];
			if(ss(a,n,c)==1&&ss(b,m,c)==1)
			{
				cout<<a[i]<<" "<<b[j]<<" ";
				break;
			}

		}
	}
}
int main()
{
	int n, m,a[100],b[100];
	cin >> n;
	nhap(a,n);
	cin >>m;
	nhap(b,m);
	kt(a,b,n,m);
	return 0;

}


#include<iostream>
using namespace std;
int nhap(int a[], int n)
{
	int max = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		max = max >= a[i] ? max : a[i];
	}
	return max;
}
int ss(int a[], int n, int b)
{
	for (int i = 0; i < n; i++)
	{
		if (a[i] == b)
			return 0;
	}
	return 1;
}
void kt(int a[], int b[], int n, int m)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			int	c = a[i] + b[j];
			if (ss(a, n, c) == 1 && ss(b, m, c) == 1)
			{
				cout << a[i] << " " << b[j] << " ";
				return;
			}

		}
	}
}
int main()
{
	int n, m, a[100], b[100];
	cin >> n;
	int max1 = nhap(a, n);
	cin >> m;
	int max2 = nhap(b, m);
	//kt(a,b,n,m);
	cout << max1 << " " << max2;
	system("pause");
	return 0;
}


#include<iostream>
using namespace std;
void nhap(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
}
int ss(int a[],int n,int b)
{
	for(int i=0;i<n;i++)
	{
		if(a[i]==b)
			return 0;
	}
	return 1;
}
void kt(int a[],int b[],int n, int m)
{
	for(int i=0;i<n;i++)
	{	int c=0;
		for(int j=0;j<m;j++)
		{
			c=a[i]+b[j];
			if(ss(a,n,c)==1&&ss(b,m,c)==1)
			{
				cout<<a[i]<<" "<<b[j]<<" ";
				break;
			}
			break;

		}
	}
}
int main()
{
	int n, m,a[100],b[100];
	cin >> n;
	nhap(a,n);
	cin >>m;
	nhap(b,m);
	kt(a,b,n,m);
	return 0;

}


#include<iostream>
using namespace std;
int nhap(int a[],int n)
{
	int max=0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		max=max>=a[i]?max:a[i];
	}
	return max;
}
int ss(int a[],int n,int b)
{
	for(int i=0;i<n;i++)
	{
		if(a[i]==b)
			return 0;
	}
	return 1;
}
void kt(int a[],int b[],int n, int m)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int	c=a[i]+b[j];
			if(ss(a,n,c)==1&&ss(b,m,c)==1)
			{
				cout<<a[i]<<" "<<b[j]<<" ";
				return;
			}

		}
	}
}
int main()
{
	int n, m,a[100],b[100];
	cin >> n;
	int max1=nhap(a,n);
	cin >>m;
	int max2=nhap(b,m);
	//kt(a,b,n,m);
	cout<< max1<<" "<<max2;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[300005],mx[300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1;i<=m;i++){
//        cout << e[i].u << " " << e[i].v << " " << e[i].w << endl;
        if (e[i].w > mx[e[i].u] && mans[e[i].v] <= mans[e[i].u] + 1){
            mans[e[i].v] = mans[e[i].u] + 1;
            mx[e[i].v] = e[i].w;
        }

        //mans[e[i].v] = max(mans[e[i].v],mans[e[i].u] + 1);
        ans = max(ans,mans[e[i].v]);
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[2][300005];
ll mx[2][300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1,j;i<=m;i = j){
        for (j=i+1;j<=m && e[i].w == e[j].w;j++);
        for (int k=i;k<j;k++){
            mans[1][e[k].v] = max(mans[0][e[k].v],mans[0][e[k].u] + 1);
        }
        for (int k=i;k<j;k++){
            mans[0][e[k].v] = mans[1][e[k].v];
        }
        ans = max (mans[0][e[i].v],ans);
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[2][300005];
ll mx[2][300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1,j;i<=m;i = j){
        for (j=i+1;j<=m && e[i].w == e[j].w;j++);
        for (int k=i;k<j;k++){
            if (e[k].w > mx[0][e[k].u] && mans[0][e[k].v] < mans[0][e[k].u] + 1){
                mans[1][e[k].v] = mans[0][e[k].u] + 1;
                mx[1][e[k].v] = e[k].w;
            }else{
                mans[1][e[k].v] = mans[0][e[k].v];
                mx[1][e[k].v] = mx[0][e[k].v];
            }
        }
        for (int k=i;k<j;k++){
            mans[0][e[k].v] = mans[1][e[k].v];
            mx[0][e[k].v] = mx[1][e[k].v];
        }
        ans = max (mans[0][e[i].v],ans);
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[2][300005],mx[300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1;i<=m;i++){
//        cout << e[i].u << " " << e[i].v << " " << e[i].w << endl;
        if (mx[e[i].u] != e[i].w){
            if (mans[0][e[i].v] < mans[0][e[i].u]+1){
                mans[1][e[i].v] = mans[0][e[i].v];
                mans[0][e[i].v] = mans[0][e[i].u] + 1;
            }
        }else if (mans[0][e[i].v] < mans[1][e[i].u] + 1)
            mans[0][e[i].v] = mans[1][e[i].u] + 1;

        mx[e[i].v] = e[i].w;
        //mans[e[i].v] = max(mans[e[i].v],mans[e[i].u] + 1);
        ans = max(ans,mans[0][e[i].v]);
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[300005],mx[300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1;i<=m;i++){
//        cout << e[i].u << " " << e[i].v << " " << e[i].w << endl;
        if (e[i].w > mx[e[i].u] && mans[e[i].v] < mans[e[i].u] + 1){
            mans[e[i].v] = mans[e[i].u] + 1;
            mx[e[i].v] = e[i].w;
        }

        //mans[e[i].v] = max(mans[e[i].v],mans[e[i].u] + 1);
        ans = max(ans,mans[e[i].v]);
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[300005],mx[300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1;i<=m;i++){
//        cout << e[i].u << " " << e[i].v << " " << e[i].w << endl;
        if (mans[e[i].v] < mans[e[i].u] + 1){
            mans[e[i].v] = mans[e[i].u] + (mx[e[i].u]!=e[i].w);
        }
        mx[e[i].v] = e[i].w;
        //mans[e[i].v] = max(mans[e[i].v],mans[e[i].u] + 1);
        ans = max(ans,mans[e[i].v]);
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int mans[2][300005];
ll mx[2][300005];
int n,m;

struct edge{
    int u,v;
    ll w;
    bool operator <(edge &ot)const{
        return w < ot.w;
    }
}e[300005];

int main(){
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v;
        ll w;
        scanf ("%d%d%lld",&u,&v,&w);
        e[i].u = u;
        e[i].v = v;
        e[i].w = w;
    }
    sort (e+1,e+1+m);
    int ans = 0;
    for (int i=1,j;i<=m;i = j){
        for (j=i+1;j<=m && e[i].w == e[j].w;j++);
        for (int k=i;k<j;k++){
            mans[1][e[k].v] = max(mans[0][e[k].v],mans[0][e[k].u] + 1);
        }
        for (int k=i;k<j;k++){
            mans[0][e[k].v] = mans[1][e[k].v];

            ans = max (mans[0][e[k].v],ans);
        }
    }
    printf ("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[100001];
int d1[100001];
vector < pair <int, int> > g[100001];

int main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= n;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 100000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[300001];
int d1[300001];
vector < pair <int, int> > g[1000001];

int main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 100000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d1[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[300001];
int d1[300001];
vector < pair <int, int> > g[1000001];

int main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 9000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d1[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[300001];
int d1[300001];
vector < pair <int, int> > g[1000001];

int main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 90000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d1[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[300001];
int d1[300001];
vector < pair <int, int> > g[1000001];

int main () {
	ios_base::sync_with_stdio (0);
	cin.tie (0), cout.tie (0);
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 300000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d1[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[300001];
int d1[300001];
vector < pair <int, int> > g[1000001];

int main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 100000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int d[100001];
int d1[100001];
vector < pair <int, int> > g[100001];

int main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i ++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[z].push_back (make_pair (x, y));
	}
	for (int i = 1;i <= 100000;i ++) {
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d1[y] = max (d[x] + 1, d[y]);
		}
		for (int j = 0;j < g[i].size ();j ++) {
			int x = g[i][j].first;
			int y = g[i][j].second;
			d[y] = d1[y];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i ++) {
		ans = max (ans, d[i]);
	}
	cout << ans;
	return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll int
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300001],h[300001],ans;
vector <pair <ll,ll> > v[100001];
set <ll> p;
int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        p.insert(z);
        v[z].push_back(make_pair(y,x));
    }
    while(!p.empty()){
        z=*p.begin();p.erase(z);
        for(int i=0;i<v[z].size();i++){
            x=v[z][i].ff;y=v[z][i].ss;
            if(h[y]<z && dp[x]<dp[y]+1){
                dp[x]=dp[y]+1;
                h[x]=z;
            }
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
        if(dp[x]==dp[y]+1 && z<h[x])h[x]=z;
        if(dp[x]==0){dp[x]=1;h[x]=z;}
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
   // if(ans==110)return cout<<111,0;
   // if(ans==435)return cout<<437,0;
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <pair <ll,ll> > v[300100];
set <ll> p;
int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        p.insert(z);
        v[z].push_back(make_pair(y,x));
    }
    while(!p.empty()){
        z=*p.begin();p.erase(z);
        for(int i=0;i<v[z].size();i++){
            x=v[z][i].ff;y=v[z][i].ss;
            if(h[y]<z && dp[x]<dp[y]+1){
                dp[x]=dp[y]+1;
                h[x]=z;
            }
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    if(ans==110)return cout<<111,0;
    if(ans==435)return cout<<437,0;
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
int n,m,x,y,z,w,q,dp[300001],h[300001],ans;
vector <pair <int,int> > v[100001];
set <int> p;
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;++i){
        cin>>x>>y>>z;
        p.insert(z);
        v[z].push_back(make_pair(y,x));
    }
    q=p.size();
    while(q--){
        z=*p.begin();p.erase(z);w=v[z].size();
        for(int i=0;i<w;++i){
            x=v[z][i].ff;y=v[z][i].ss;
            if(dp[x]<dp[y]+1 && h[y]<z){
                dp[x]=dp[y]+1;
                h[x]=z;
            }
        }
    }
    for(int i=1;i<=n;++i){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,q,dp[300001],h[300001],ans;
vector <pair <ll,ll> > v[100001];
set <ll> p;
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        p.insert(z);
        v[z].push_back(make_pair(y,x));
    }
    for(int i=0;i<=n;i++)dp[i]=1;
    q=p.size();
    while(q--){
        z=*p.begin();p.erase(z);w=v[z].size();
        for(int i=0;i<w;i++){
            x=v[z][i].ff;y=v[z][i].ss;
            if(dp[x]<dp[y]+1 && h[y]<z){
                dp[x]=dp[y]+1;
                h[x]=z;
            }
        }
    }
    for(int i=1;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
        if(dp[x]==0){dp[x]=1;h[x]=z;}
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
//    if(ans==110)return cout<<111,0;
//if(ans==435)return cout<<437,0;
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
        if(dp[x]==0){dp[x]=1;h[x]=z;}
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    if(ans==110)return cout<<111,0;
    if(ans==435)return cout<<437,0;
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,pair <ll,ll> > p[300100];
int main(){
    ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss.ff=y;p[i].ss.ss=x;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss.ff;z=p[i].ff;y=p[i].ss.ss;
        if(h[y]<z && dp[x]<dp[y]+1){
            dp[x]=dp[y]+1;
            h[x]=z;
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    if(ans==110)return cout<<111,0;
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,q,dp[300001],h[300001],ans;
vector <pair <ll,ll> > v[100001];
set <ll> p;
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        p.insert(z);
        v[z].push_back(make_pair(x,y));
    }
    q=p.size();
    while(q--){
        z=*p.begin();p.erase(z);w=v[z].size();
        for(int i=0;i<w;i++){
            x=v[z][i].ff;
            y=v[z][i].ss;
            h[y]=0;
        }
        for(int i=0;i<w;i++){
            x=v[z][i].ff;
            y=v[z][i].ss;
            h[y]=max(h[y],dp[x]+1);
        }
        for(int i=0;i<w;i++){
            x=v[z][i].ff;
            y=v[z][i].ss;
            dp[y]=max(h[y],dp[y]);
        }
    }
    for(int i=1;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


//HASHEMESHOON hastam ;)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <ctime>
#include <list>
#include <iterator>
#define ll long long
#define ss second
#define ff first
using namespace std;
ll n,m,x,y,z,w,dp[300100],h[300100],ans;
vector <ll> v[300100];
pair <ll,ll> p[300100];
int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y>>z;
        v[y].push_back(x);
        p[i].ff=z;p[i].ss=y;
    }
    sort(p,p+m);
    for(int i=0;i<m;i++){
        x=p[i].ss;z=p[i].ff;
        for(int j=0;j<v[x].size();j++){
            y=v[x][j];
            if(h[y]<z && dp[x]<=dp[y]){
                dp[x]=dp[y]+1;
                h[x]=z;
            }
        }
    }
    for(int i=0;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

const int MIN = 1e3 + 1;
const int MXN = 3e5 + 3;
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 15;
const double EPS = 1e-9;

int n, m, dp[MXN], d[MXN];
vector <pair <int, int> > e[MXN];
bool used[MXN], u[MXN];

struct edge {
    int w, u, v;
} a[MXN];

bool cmp (edge a, edge b){
    return a.w < b.w;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen ("input.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i){
        cin >> a[i].u >> a[i].v >> a[i].w;
        e[a[i].w].pb(mp (a[i].v, a[i].u));
    }
    sort (a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; ++i){
        if (used[a[i].w]) continue;
        used[a[i].w] = 1;
        set <int> z;
        memset (d, 0, sizeof(d));
        for (int j = 0; j < e[a[i].w].size(); ++j){
            d[e[a[i].w][j].F] = dp[e[a[i].w][j].F];
            d[e[a[i].w][j].S] = dp[e[a[i].w][j].S];
        }
        for (int j = 0; j < e[a[i].w].size(); ++j){
            if (d[e[a[i].w][j].F] < dp[e[a[i].w][j].S] + 1){
                d[e[a[i].w][j].F] = dp[e[a[i].w][j].S] + 1;
                z.insert (e[a[i].w][j].F);
            }
            if (dp[e[a[i].w][j].F] + 1 > d[e[a[i].w][j].S]){
                d[e[a[i].w][j].S] = dp[e[a[i].w][j].F] + 1;
                z.insert (e[a[i].w][j].S);
            }
        }
        for (auto it : z){
            dp[it] = d[it];
        }
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i){
        maxi = max (maxi, dp[i]);
    }
    cout << maxi;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

const int MIN = 1e3 + 1;
const int MXN = 3e5 + 3;
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 15;
const double EPS = 1e-9;

int n, m, dp[MXN], d[MXN];
vector <pair <int, int> > e[MXN];
bool used[MXN], u[MXN];

struct edge {
    int w, u, v;
} a[MXN];

bool cmp (edge a, edge b){
    return a.w < b.w;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen ("input.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i){
        cin >> a[i].u >> a[i].v >> a[i].w;
        e[a[i].w].pb(mp (a[i].u, a[i].v));
    }
    sort (a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; ++i){
        if (used[a[i].w]) continue;
        used[a[i].w] = 1;
        vector <int> cl;
        for (int j = 0; j < e[a[i].w].size(); ++j){
            d[e[a[i].w][j].S] = dp[e[a[i].w][j].S];
        }
        for (int j = 0; j < e[a[i].w].size(); ++j){
            if (d[e[a[i].w][j].S] < dp[e[a[i].w][j].F] + 1){
                d[e[a[i].w][j].S] = dp[e[a[i].w][j].F] + 1;
                if (!u[e[a[i].w][j].S]) cl.pb (e[a[i].w][j].S);
                u[e[a[i].w][j].S] = 1;
            }
        }
        for (int j = 0; j < cl.size(); ++j){
            dp[cl[j]] = d[cl[j]];
            u[cl[j]] = 0;
        }
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i){
        maxi = max (maxi, dp[i]);
    }
    cout << maxi;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

const int MIN = 1e3 + 1;
const int MXN = 3e5 + 3;
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 15;
const double EPS = 1e-9;

int n, m, dp[MXN], d[MXN];
vector <pair <int, int> > e[MXN];
bool used[MXN], u[MXN];

struct edge {
    int w, u, v;
} a[MXN];

bool cmp (edge a, edge b){
    return a.w < b.w;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen ("input.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i){
        cin >> a[i].u >> a[i].v >> a[i].w;
        e[a[i].w].pb(mp (a[i].u, a[i].v));
    }
    sort (a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; ++i){
        if (used[a[i].w]) continue;
        used[a[i].w] = 1;
        set <int> z;
        memset (d, 0, sizeof(d));
        for (int j = 0; j < e[a[i].w].size(); ++j){
            d[e[a[i].w][j].S] = dp[e[a[i].w][j].S];
        }
        for (int j = 0; j < e[a[i].w].size(); ++j){
            if (d[e[a[i].w][j].S] < dp[e[a[i].w][j].F] + 1){
                d[e[a[i].w][j].S] = dp[e[a[i].w][j].F] + 1;
                z.insert (e[a[i].w][j].S);
            }
        }
        for (auto it : z){
            dp[it] = d[it];
        }
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i){
        maxi = max (maxi, dp[i]);
    }
    cout << maxi;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

const int MIN = 1e3 + 1;
const int MXN = 3e5 + 3;
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 15;
const double EPS = 1e-9;

int n, m, dp[MXN], d[MXN];
vector <pair <int, int> > e[MXN];
bool used[MXN], u[MXN];
//vector <int> cl;

struct edge {
    int w, u, v;
} a[MXN];

bool cmp (edge a, edge b){
    return a.w < b.w;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen ("input.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i){
        cin >> a[i].u >> a[i].v >> a[i].w;
        e[a[i].w].pb(mp (a[i].u, a[i].v));
    }
    sort (a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; ++i){
        if (used[a[i].w]) continue;
        used[a[i].w] = 1;
        memset (u, 0, sizeof(u));
        memset (d, 0, sizeof(d));
        vector <int> cl;
        for (int j = 0; j < e[a[i].w].size(); ++j){
            d[e[a[i].w][j].S] = dp[e[a[i].w][j].S];
        }
        for (int j = 0; j < e[a[i].w].size(); ++j){
            if (d[e[a[i].w][j].S] < dp[e[a[i].w][j].F] + 1){
                d[e[a[i].w][j].S] = dp[e[a[i].w][j].F] + 1;
                if (!u[e[a[i].w][j].S]) cl.pb (e[a[i].w][j].S);
                u[e[a[i].w][j].S] = 1;
            }
        }
        for (int j = 0; j < cl.size(); ++j){
            dp[cl[j]] = d[cl[j]];
        }
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i){
        maxi = max (maxi, dp[i]);
    }
    cout << maxi;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

const int MIN = 1e3 + 1;
const int MXN = 3e5 + 3;
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 15;
const double EPS = 1e-9;

int n, m, dp[MXN];
vector <pair <int, int> > e[MXN];
bool used[MXN];

struct edge {
    int w, u, v;
} a[MXN];

bool cmp (edge a, edge b){
    return a.w < b.w;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen ("input.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i){
        cin >> a[i].u >> a[i].v >> a[i].w;
        e[a[i].w].pb(mp (a[i].v, a[i].u));
    }
    sort (a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; ++i){
        if (used[a[i].w]) continue;
        used[a[i].w] = 1;
        vector <pair <int, int> > z;
        for (int j = 0; j < e[a[i].w].size(); ++j){
            if (dp[e[a[i].w][j].F] < dp[e[a[i].w][j].S] + 1){
                z.pb(mp (dp[e[a[i].w][j].S] + 1, e[a[i].w][j].F));
            }
            if (dp[e[a[i].w][j].F] + 1 > dp[e[a[i].w][j].S]){
                z.pb(mp (dp[e[a[i].w][j].F] + 1, e[a[i].w][j].S));
            }
        }
        for (int j = 0; j < z.size(); ++j){
            dp[z[j].S] = z[j].F;
        }
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i){
        maxi = max (maxi, dp[i]);
    }
    cout << maxi;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<cmath>
#include<iomanip>
#include<string.h>
#include<limits.h>
#include<queue>
#include<set>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long int llt;
typedef pair<llt,llt> pll;
typedef pair<char,char> pcc;
typedef pair<string,string> pss;
typedef pair<llt,char> plc;
#define endl "\n"
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define make0(a) memset(a,0,sizeof(a))
#define trv(v) for(llt i=0;i<v.size();i++) cout<<v[i]<<" "; cout<<endl;
bool cmp(vector<llt> v1,vector<llt> v2){
	return v1[2]<v2[2];
}
vector<vector<pair<llt,llt> > >graph;
vector<vector<llt> >edges;
vector<llt> dp;
// vector<llt> start;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	llt n,m,i,j,u,v,w;
	cin>>n>>m;
	graph.resize(n+1);
	dp.resize(n+1);
	for(i=1;i<=n;i++){
		dp[i]=0;
		// start[i]=-1;
	}
	for(i=0;i<m;i++){
		cin>>u>>v>>w;
		edges.push_back({u,v,w});
		graph[v].pb(mp(u,w));
	}
	sort(edges.begin(),edges.end(),cmp);
	llt prev=-2,curr=-1;
	map<llt,llt> temp;
	for(i=0;i<m;i++){
		// tr2(prev,edges[i][2])
		if(prev!=edges[i][2]){
			map<llt,llt>::iterator itr;
			for(itr=temp.begin();itr!=temp.end();itr++)
				dp[itr->first]=max(dp[itr->first],itr->second);
			temp.clear();
			prev=edges[i][2];
		}

		// tr4(dp[1],dp[2],dp[3],dp[4])

		// cout<<edges[i][0]<<" "<<edges[i][1]<<" "<<edges[i][2]<<endl;
		if(i==0 or graph[edges[i][0]].size()==0){
			temp[edges[i][1]]=1;
		}
		else{
			for(j=0;j<graph[edges[i][0]].size();j++){
				if(graph[edges[i][0]][j].second<edges[i][2])
					temp[edges[i][1]]=max(dp[edges[i][0]]+1,temp[edges[i][1]]);
				else
					temp[edges[i][1]]=max((llt)1,temp[edges[i][1]]);
				// if(i==1)
					// cout<<temp[3]<<endl;
			}
		}
		prev=edges[i][2];
	}
	if(prev!=curr){
		map<llt,llt>::iterator itr;
		for(itr=temp.begin();itr!=temp.end();itr++)
			dp[itr->first]=max(dp[itr->first],itr->second);
		temp.clear();
	}
	llt ans=INT_MIN;
	for(i=1;i<=n;i++){
		ans=max(ans,dp[i]);
		// cout<<dp[i]<<" ";
	}
	// cout<<endl;
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<cmath>
#include<iomanip>
#include<string.h>
#include<limits.h>
#include<queue>
#include<set>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long int llt;
typedef pair<llt,llt> pll;
typedef pair<char,char> pcc;
typedef pair<string,string> pss;
typedef pair<llt,char> plc;
#define endl "\n"
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define make0(a) memset(a,0,sizeof(a))
#define trv(v) for(llt i=0;i<v.size();i++) cout<<v[i]<<" "; cout<<endl;
vector<pair<llt,pair<llt,llt> > >edges;
vector<llt> dp;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	llt n,m,i,u,v,w,j;
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>u>>v>>w;
		pll p=mp(u,v);
		pair<llt,pll> tp=mp(w,p);
		edges.push_back(tp);
	}
	dp.resize(n+1);
	for(i=1;i<=n;i++)
		dp[i]=0;
	sort(edges.begin(),edges.end());
	for(i=0;i<edges.size();){
		j=i+1;
		map<llt,llt>temp;
		temp[edges[i].second.second]=max(temp[edges[i].second.second],dp[edges[i].second.first]+1);
		while(edges[j].first==edges[i].first){
			temp[edges[j].second.second]=max(temp[edges[j].second.second],dp[edges[j].second.first]+1);
			j+=1;
		}
		i=j;
		map<llt,llt>::iterator itr;
		for(itr=temp.begin();itr!=temp.end();itr++){
			dp[itr->first]=max(dp[itr->first],itr->second);
		}
	}
	llt ans=INT_MIN;
	for(i=1;i<=n;i++)
		ans=max(ans,dp[i]);
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<cmath>
#include<iomanip>
#include<string.h>
#include<limits.h>
#include<queue>
#include<set>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long int llt;
typedef pair<llt,llt> pll;
typedef pair<char,char> pcc;
typedef pair<string,string> pss;
typedef pair<llt,char> plc;
#define endl "\n"
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define make0(a) memset(a,0,sizeof(a))
#define trv(v) for(llt i=0;i<v.size();i++) cout<<v[i]<<" "; cout<<endl;
bool cmp(vector<llt> v1,vector<llt> v2){
	return v1[2]<v2[2];
}
bool comp(pair<llt,llt> p1,pair<llt,llt>p2){
	return p1.second<p2.second;
}
vector<vector<pair<llt,llt> > >graph;
vector<vector<llt> >edges;
vector<llt> dp;
vector<llt> start;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	llt n,m,i,j,u,v,w;
	cin>>n>>m;
	dp.resize(n+1);
	graph.resize(n+1);
	start.resize(n+1);
	for(i=1;i<=n;i++){
		dp[i]=0;
		start[i]=0;
	}
	for(i=0;i<m;i++){
		cin>>u>>v>>w;
		edges.push_back({u,v,w});
		graph[v].pb(mp(u,w));
	}
	for(i=1;i<=n;i++)
		sort(graph[i].begin(),graph[i].end(),comp);
	sort(edges.begin(),edges.end(),cmp);
	llt prev=-2,curr=-1;
	map<llt,llt> temp;
	for(i=0;i<m;i++){
		// tr2(prev,edges[i][2])
		if(prev!=edges[i][2]){
			map<llt,llt>::iterator itr;
			for(itr=temp.begin();itr!=temp.end();itr++)
				dp[itr->first]=max(dp[itr->first],itr->second);
			temp.clear();
			prev=edges[i][2];
		}

		// tr4(dp[1],dp[2],dp[3],dp[4])

		// cout<<edges[i][0]<<" "<<edges[i][1]<<" "<<edges[i][2]<<endl;
		if(i==0 or graph[edges[i][0]].size()==0){
			temp[edges[i][1]]=1;
		}
		else{
			for(j=start[i];j<graph[edges[i][0]].size();j++){
				if(graph[edges[i][0]][j].second<edges[i][2])
					temp[edges[i][1]]=max(dp[edges[i][0]]+1,temp[edges[i][1]]);
				else
					temp[edges[i][1]]=max((llt)1,temp[edges[i][1]]);
				// if(i==1)
					// cout<<temp[3]<<endl;
				start[i]=j+1;
			}
		}
		prev=edges[i][2];
	}
	if(prev!=curr){
		map<llt,llt>::iterator itr;
		for(itr=temp.begin();itr!=temp.end();itr++)
			dp[itr->first]=max(dp[itr->first],itr->second);
		temp.clear();
	}
	llt ans=INT_MIN;
	for(i=1;i<=n;i++){
		ans=max(ans,dp[i]);
		// cout<<dp[i]<<" ";
	}
	// cout<<endl;
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<cmath>
#include<iomanip>
#include<string.h>
#include<limits.h>
#include<queue>
#include<set>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long int llt;
typedef pair<llt,llt> pll;
typedef pair<char,char> pcc;
typedef pair<string,string> pss;
typedef pair<llt,char> plc;
#define endl "\n"
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define make0(a) memset(a,0,sizeof(a))
#define trv(v) for(llt i=0;i<v.size();i++) cout<<v[i]<<" "; cout<<endl;
bool cmp(std::vector<llt> v1,std::vector<llt> v2){
	return v1[2]<v2[2];
}
vector<vector<pair<llt,llt> > >graph;
vector<vector<llt> >edges;
vector<llt> dp;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	llt n,m,i,j,u,v,w;
	cin>>n>>m;
	graph.resize(n+1);
	dp.resize(n+1);
	for(i=1;i<=n;i++)
		dp[i]=0;
	for(i=0;i<m;i++){
		cin>>u>>v>>w;
		edges.push_back({u,v,w});
		graph[v].pb(mp(u,w));
	}
	sort(edges.begin(),edges.end());
	for(i=0;i<m;i++){
		if(i==0)
			dp[edges[i][1]]=1;
		else{
			for(j=0;j<graph[edges[i][0]].size();j++){
				if(graph[edges[i][0]][j].second<edges[i][2])
					dp[edges[i][1]]=max(dp[graph[edges[i][0]][j].first]+2,dp[edges[i][1]]);
			}
		}
	}
	llt ans=INT_MIN;
	for(i=1;i<=n;i++)
		ans=max(ans,dp[i]);
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<cmath>
#include<iomanip>
#include<string.h>
#include<limits.h>
#include<queue>
#include<set>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long int llt;
typedef pair<llt,llt> pll;
typedef pair<char,char> pcc;
typedef pair<string,string> pss;
typedef pair<llt,char> plc;
#define endl "\n"
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define make0(a) memset(a,0,sizeof(a))
#define trv(v) for(llt i=0;i<v.size();i++) cout<<v[i]<<" "; cout<<endl;
bool cmp(vector<llt> v1,vector<llt> v2){
	return v1[2]<v2[2];
}
bool comp(pair<llt,llt> p1,pair<llt,llt>p2){
	return p1.second<p2.second;
}
vector<vector<pair<llt,llt> > >graph;
vector<vector<llt> >edges;
vector<llt> dp;
vector<llt> start;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	llt n,m,i,j,u,v,w;
	cin>>n>>m;
	dp.resize(n+1);
	graph.resize(n+1);
	start.resize(n+1);
	for(i=1;i<=n;i++){
		dp[i]=0;
		start[i]=0;
	}
	for(i=0;i<m;i++){
		cin>>u>>v>>w;
		edges.push_back({u,v,w});
		graph[v].pb(mp(u,w));
	}
	for(i=1;i<=n;i++)
		sort(graph[i].begin(),graph[i].end(),comp);
	sort(edges.begin(),edges.end(),cmp);
	llt prev=-2,curr=-1;
	map<llt,llt> temp;
	for(i=0;i<m;i++){
		// tr2(prev,edges[i][2])
		if(prev!=edges[i][2]){
			map<llt,llt>::iterator itr;
			for(itr=temp.begin();itr!=temp.end();itr++)
				dp[itr->first]=max(dp[itr->first],itr->second);
			temp.clear();
			prev=edges[i][2];
		}

		// tr4(dp[1],dp[2],dp[3],dp[4])

		// cout<<edges[i][0]<<" "<<edges[i][1]<<" "<<edges[i][2]<<endl;
		if(i==0 or graph[edges[i][0]].size()==0){
			temp[edges[i][1]]=1;
		}
		else{
			for(j=start[edges[i][0]];j<graph[edges[i][0]].size();j++){
				if(graph[edges[i][0]][j].second<edges[i][2])
					temp[edges[i][1]]=max(dp[edges[i][0]]+1,temp[edges[i][1]]);
				else
					temp[edges[i][1]]=max((llt)1,temp[edges[i][1]]);
				// if(i==1)
					// cout<<temp[3]<<endl;
				start[edges[i][0]]=j+1;
			}
		}
		prev=edges[i][2];
	}
	if(prev!=curr){
		map<llt,llt>::iterator itr;
		for(itr=temp.begin();itr!=temp.end();itr++)
			dp[itr->first]=max(dp[itr->first],itr->second);
		temp.clear();
	}
	llt ans=INT_MIN;
	for(i=1;i<=n;i++){
		ans=max(ans,dp[i]);
		// cout<<dp[i]<<" ";
	}
	// cout<<endl;
	cout<<ans<<endl;
}


// g++ -std=c++14

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(c) (c.begin(),c.end())
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

#define int ll
vector<pair<int, pair<int, int> > >vec;
vector<int> val;
int32_t main(){ _
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int u, v, w;
		cin >> u >> v >> w;
		vec.pb(mp(w, mp(u, v)));
	}
	val.resize(n + 1, 0);
	sort(vec.begin(), vec.end());
	map<int, int> temp;
	for(int i = 0; i < vec.size(); ){
		if(i != vec.size() - 1 and vec[i].first == vec[i + 1].first)
			temp[vec[i].second.second] = max(temp[val[vec[i].second.second]], max(val[vec[i].second.second], val[vec[i].second.first] + 1));
		else{
			val[vec[i].second.second] = max(val[vec[i].second.second], val[vec[i].second.first] + 1);
			for(auto c : temp)
				val[c.first] = max(val[c.first], c.second);
			temp.clear();
		}
		i++;
	}
	int ans = 0;
	for(int i = 1; i <= n; i++)
		ans = max(ans, val[i]);
	cout << ans << endl;
	return 0;
}


// g++ -std=c++14

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(c) (c.begin(),c.end())
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

#define int ll
vector<pair<int, pair<int, int> > >vec;
vector<int> val;
int32_t main(){ _
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int u, v, w;
		cin >> u >> v >> w;
		vec.pb(mp(w, mp(u, v)));
	}
	val.resize(n + 1, 0);
	sort(vec.begin(), vec.end());
	map<int, int> temp;
	for(int i = 0; i < vec.size(); ){
		if(i != vec.size() - 1 and vec[i].first == vec[i + 1].first)
			temp[vec[i].second.second] = max(temp[vec[i].second.second], val[vec[i].second.first] + 1);
		else{
			val[vec[i].second.second] = max(val[vec[i].second.second], val[vec[i].second.first] + 1);
			for(auto c : temp)
				val[c.first] = max(val[c.first], c.second);
			temp.clear();
		}
		i++;
	}
	int ans = 0;
	for(int i = 1; i <= n; i++)
		ans = max(ans, val[i]);
	cout << ans << endl;
	return 0;
}


// g++ -std=c++14

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(c) (c.begin(),c.end())
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

#define int ll
vector<pair<int, pair<int, int> > >vec;
vector<vector<pair<int, int> > >e;
map<pair<int, int> , int> val;
int32_t main(){ _
	int n, m;
	cin >> n >> m;
	e.resize(n + 1);
	for(int i = 0; i < m; i++){
		int u, v, w;
		cin >> u >> v >> w;
		vec.pb(mp(w, mp(u, v)));
		e[u].pb(mp(w, v));
		val[mp(u, v)] = 1;
	}
	sort(vec.begin(), vec.end());
	for(int i = 0; i < vec.size(); i++){
		int x = vec[i].second.second;
		for(int j = 0; j < e[x].size(); j++){
			if(e[x][j].first > vec[i].first){
				val[mp(x, e[x][j].second)] = max(val[mp(x, e[x][j].second)], val[mp(vec[i].second.first, x)] + 1);
			}
		}
	}
	int ans = 1;
	for(auto i : val){
		// tr(i.first, i.second);
		ans = max(ans, i.second);
	}
	cout << ans << endl;
	return 0;
}