#define forn(i,n) for(int i=0;i<n;++i)
#include<cstdio>
#include<map>
#include<iostream>
#include<fstream>
#define scanf scanf_s

using namespace std;

inline int min(int a, int b)
{
    if (a < b)return a;
    return b;
}

inline int max(int a, int b)
{
    if (a > b)return a;
    return b;
}

int main()
{
    int n, m;
    std::cin >> n >> m;
    int mi;
    int r=0;
    int t;
    forn(i, n)
    {
        mi = 1e9+3;
        forn(j, m)
        {
            scanf("%d", &t);
            //cout << t << ' ';
            mi = min(mi, t);
        }
        r = max(mi, t);
    }
    cout << r;
//  cin >> n;
}


#define forn(i,n) for(int i=0;i<n;++i)
#include<cstdio>
#include<map>
#include<iostream>
#include<fstream>
#define scanf scanf_s

using namespace std;

inline int min(int a, int b)
{
    if (a < b)return a;
    return b;
}

inline int max(int a, int b)
{
    if (a > b)return a;
    return b;
}

int main()
{
    int n, m;
    std::cin >> n >> m;
    int mi;
    int r=0;
    int t;
    forn(i, n)
    {
        mi = 1e9+3;
        forn(j, m)
        {
            scanf("%d", &t);
            //cout << t << ' ';
            mi = min(mi, t);
        }
    //  cout << mi << ' ';
        r = max(mi, r);
    }
    cout << r;
//  cin >> n;
}


#include <iostream>
#include <string>
#include <climits>

using namespace std;

int main()
{
	long long n, m, maxx = 0;
	cin >> n >> m1;
	for (int i = 0; i < n; i++)
	{
		long long minn = LLONG_MAX;
		for (int j = 0; j < m; j++)
		{
			long long q;
			cin >> q;
			if (q < minn)
				minn = q;
		}
		if (minn > maxx)
			maxx = minn;
	}
	cout << maxx << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <climits>

using namespace std;

int main()
{
	long long n, m, maxx = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		long long minn = LLONG_MAX;
		for (int j = 0; j < m; j++)
		{
			long long q;
			cin >> q;
			if (q < minn)
				minn = q;
		}
		if (minn > maxx)
			maxx = minn;
	}
	cout << maxx << endl;
	return 0;
}


#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<utility>
#include<map>
#include <unordered_map>
#include <bitset>
using namespace std;
typedef long long ll;
int main() {
	int i, j, R, C, A[104][104];
	cin >> R >> C;
	for (i = 1; i <= R; i++) for (j = 1; j <= C; j++) cin >> A[i][j];
	int ans = 0;
	for (i = 1; i <= R; i++) {
		int a;
		a = A[i][1];
		for (j = 2; j <= C; j++) {
			if (A[i][j] < a) a = A[i][j];
		}
		if (ans < a) ans = a;
	}
	cout << ans;
}


#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<utility>
#include<map>
#include <unordered_map>
#include <bitset>
using namespace std;
typedef long long ll;
int main() {
	int i, j, R, C, A[104][104];
	cin >> R >> C;
	for (i = 1; i <= R; i++) for (j = 1; j <= C; j++) cin >> A[i][j];
	int ans = 0;
	for (j = 1; j <= C; j++) {
		int a;
		a = A[1][j];
		for (i = 2; i <= R; i++) {
			if (A[i][j] < a) a = A[i][j];
		}
		if (ans < a) ans = a;
	}
	cout << ans;
}


#include <iomanip>
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>

using namespace std;
long float x[100][100];
float v[100];
int main(){
	int n, m;
	cin >> n >> m;

		for (int i = 0; i < n; i++){
			v[i] = 1000000000;
			for (int j = 0; j < m; j++){

			cin >> x[i][j];
		}

		}float h = 1000000000;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				if (v[i]>x[i][j])v[i] = x[i][j];
			}

		}
		sort(v, v + n);
		cout << v[n-1];

 }


// ConsoleApplication134.cpp : Defines the entry point for the console application.
//

#include <iomanip>
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>

using namespace std;
long float x[100][100];
long long v[100];
int main(){
	int n, m;
	cin >> n >> m;

		for (int i = 0; i < n; i++){
			v[i] = 1000000000;
			for (int j = 0; j < m; j++){

			cin >> x[i][j];
		}

		}float h = 1000000000;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				if (v[i]>x[i][j])v[i] = x[i][j];
			}

		}
		sort(v, v + n);
		cout << v[n-1];

 }


#include <iostream>
#include <vector>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
   long long in[105][105];
    long long min = 1e9;
    vector <int> m_min(n);
    for (int i = 0; i < n; i++)
    {
        min = 1e9;
        for (int j = 0; j < m; j++)
        {
            cin >> in[i][j];
            if (in[i][j]<min) min = in[i][j];
        }
        m_min[i] = min;
    }
    long long i_max = -1;
   int max = -1e9;
    for (int i = 0; i < n;i++)
        if (m_min[i]>max) {
            max = m_min[i];
            i_max = i;
        }
    cout << max;
    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    int in[105][105];
    int max = -1e9;
    vector <int> m_max(n);
    for (int i = 0; i < n; i++)
    {
        max = -1e9;
        for (int j = 0; j < m; j++)
        {
            cin >> in[i][j];
            if (in[i][j]>max) max = in[i][j];
        }
        m_max[i] = max;
    }
    int i_max = -1;
    max = -1e9;
    for (int i = 0; i < n;i++)
        if (m_max[i]>max) {
            max = m_max[i];
            i_max = i;
        }
    int min = 1e9, i_min = -1;
    for (int i = 0; i < n;i++)
        if (in[i][i_max] < min) {
            min = in[i][i_max];
            i_min = i;
        }
    cout << in[i_min][i_max];
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    for (int i=0;i<n; i++)
    {
        for (int j=0;j<m; j++)
        {
            cin>>a[i][j];
        }
    }
    vector <int> b;
    for (int i=0; i<n; i++)
    {
        b[i]=a[0][j];
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m;j++)
        {
            if (b[i]>a[i][j]) {b[i]=a[i][j];}

        }
    }
    sort (b.begin(),b.end());
    cout<<b[0];
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];

    for (int i=0;i<n; i++)
    {
        for (int j=0;j<m; j++)
        {
            cin>>a[i][j];
        }
    }
    vector <int> b;
    for (int i=0; i<n; i++)
    {
        b.push_back(a[i][0]);
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m;j++)
        {
            if (b[i]>a[i][j]) {b[i]=a[i][j];}

        }
    }
    sort (b.begin(),b.end());
    cout<<b[n-1];
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int main()
{
    int n,m;
    vector <vector <int>> a;
    cin>>n>>m;
    for (int i=0; i<n; i++)
    {
        vector <int> temp;
        for (int j=0; j<m; j++)
        {
            int karl;
            cin>>karl;
            temp.push_back(karl);
        }
        a.push_back(temp);
    }

    //for (int i=0;i<n; i++)
    //{
      //  for (int j=0;j<m; j++)
        //{
          //  cin>>a[i][j];
        //}
    //}
    vector <int> b;
    for (int i=0; i<n; i++)
    {
        b.push_back(a[i][0]);
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m;j++)
        {
            if (b[i]>a[i][j]) {b[i]=a[i][j];}

        }
    }
    sort (b.begin(),b.end());
    cout<<b[n-1];
    return 0;
}


#include<iostream>
using namespace std;

const int MM = 105;
const int MAX = 99999999;
int main ()
{
	int C[MM][MM];
	int MIN[MM];
	int N, M;
	int i, j, min_X;
	while(cin>>N>>M)
	{
		for(i = 0; i < N; i++)
			for(j = 0; j < M; j++)
				cin>>C[i][j];
		for(j = 0; j < M; j++)
		{
			MIN[j] = MAX;
			for(i = 0; i < N; i++)
				if(C[i][j] < MIN[j])
					MIN[j] = C[i][j];
		}
		for(j = 0, min_X = 0; j < M; j++)
			if(MIN[j] > min_X)
				min_X = MIN[j];
		cout<<min_X<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;

const int MM = 105;
const int MAX = 99999999;
int main ()
{
	int C[MM][MM];
	int MIN[MM];
	int N, M;
	int i, j, min_X;
	while(cin>>N>>M)
	{
		for(i = 0; i < N; i++)
			for(j = 0; j < M; j++)
				cin>>C[i][j];
		for(i = 0; i < N; i++)
		{
			MIN[i] = MAX;
			for(j = 0; j < M; j++)
				if(C[i][j] < MIN[i])
					MIN[i] = C[i][j];
		}
		for(i = 0, min_X = 0; i < N; i++)
			if(MIN[i] > min_X)
				min_X = MIN[i];
		cout<<min_X<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;

const int MM = 105;
const int MAX = 1000000000+5;
int main ()
{
	int C[MM][MM];
	int MIN[MM];
	int N, M;
	int i, j, min_X;
	while(cin>>N>>M)
	{
		for(i = 0; i < N; i++)
			for(j = 0; j < M; j++)
				cin>>C[i][j];
		for(i = 0; i < N; i++)
		{
			MIN[i] = MAX;
			for(j = 0; j < M; j++)
				if(C[i][j] < MIN[i])
					MIN[i] = C[i][j];
		}
		for(i = 0, min_X = 0; i < N; i++)
			if(MIN[i] > min_X)
				min_X = MIN[i];
		cout<<min_X<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;

const int MM = 105;
const int MAX = 999999999;
int main ()
{
	int C[MM][MM];
	int MIN[MM];
	int N, M;
	int i, j, min_X;
	while(cin>>N>>M)
	{
		for(i = 0; i < N; i++)
			for(j = 0; j < M; j++)
				cin>>C[i][j];
		for(i = 0; i < N; i++)
		{
			MIN[i] = MAX;
			for(j = 0; j < M; j++)
				if(C[i][j] < MIN[i])
					MIN[i] = C[i][j];
		}
		for(i = 0, min_X = 0; i < N; i++)
			if(MIN[i] > min_X)
				min_X = MIN[i];
		cout<<min_X<<endl;
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,num;
char mp[1010][1010];
int vis[1010][1010],flag[1000010];
__int64 rowmin[1010];
struct node
{
    int x,y;
};
int xx[4]= {0,0,1,-1};
int yy[4]= {1,-1,0,0};
queue<node>q;
void bfs(int x,int y,int num)
{
    while(!q.empty()) q.pop();
    node now,next;
    now.x=x;
    now.y=y;
    vis[now.x][now.y]=num;
    flag[num]++;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next.x=now.x+xx[i];
            next.y=now.y+yy[i];
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&mp[next.x][next.y]=='.'&&vis[next.x][next.y]==-1)
            {
                vis[next.x][next.y]=num;
                flag[num]++;
                q.push(next);
            }
        }
    }
    return ;
}
int main()
{
    int a;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            rowmin[i]=0x3f3f3f3f;
            for(int j=1;j<=m;j++)
            {
                scanf("%I64d",&a);
                if(a<rowmin[i])
                    rowmin[i]=a;
            }
        }
        __int64 ans=0;
        for(int i=1;i<=n;i++)
            ans=max(ans,rowmin[i]);
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,num;
char mp[1010][1010];
int vis[1010][1010],flag[1000010];
int rowmin[1010];
struct node
{
    int x,y;
};
int xx[4]= {0,0,1,-1};
int yy[4]= {1,-1,0,0};
queue<node>q;
void bfs(int x,int y,int num)
{
    while(!q.empty()) q.pop();
    node now,next;
    now.x=x;
    now.y=y;
    vis[now.x][now.y]=num;
    flag[num]++;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next.x=now.x+xx[i];
            next.y=now.y+yy[i];
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&mp[next.x][next.y]=='.'&&vis[next.x][next.y]==-1)
            {
                vis[next.x][next.y]=num;
                flag[num]++;
                q.push(next);
            }
        }
    }
    return ;
}
int main()
{
    int a;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            rowmin[i]=0x3f3f3f;
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a);
                if(a<rowmin[i])
                    rowmin[i]=a;
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
            ans=max(ans,rowmin[i]);
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
const int maxn=0xffffffff;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                cin>>c;
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn=1000000000;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn=0xffffffff;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn=1000000005;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn=10000000;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e9+5;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn=10000000;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
const int maxn=1000000000;
int main()
{
    int n,m;
    while(true)
    {
        cin>>n;
        cin>>m;
        int ans=0;
        for(int i=0;i<n;++i)
        {
            int tp=maxn;
            for(int j=0;j<m;++j)
            {
                int c;
                cin>>c;
                tp=min(tp,c);
            }
            ans=max(ans,tp);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <list>

using namespace std;
typedef long long ll;

int main()
{
#ifdef _CONSOLE
    freopen("input.txt", "r", stdin);
#endif
    string a, b;
    cin >> a >> b;
    int firstnonzero1 = 0;
    int firstnonzero2 = 0;
    int siz1 = a.size();
    int siz2 = b.size();
    bool isnotzero1 = false;
    bool isnotzero2 = false;
    for (int i = 0; i < siz1; i++)
    {
        if (a[i] != '0') {
            firstnonzero1 = i;
            isnotzero1 = true;
            break;
        }
    }
    for (int i = 0; i <siz2; i++)
    {
        if (b[i] != '0') {
            firstnonzero2 = i;
            isnotzero2 = true;
            break;
        }
    }
    if (isnotzero1 == false && isnotzero2 == true)
    {
        cout << "<" << endl;
        return 0;
    }
    else if (isnotzero1 == true && isnotzero2 == false)
    {
        cout << ">" << endl;
        return 0;
    }
    else if (isnotzero1 == false && isnotzero2 == false)
    {
        cout << "=" << endl;
        return 0;
    }
    if ((siz1 - firstnonzero1)==(siz2 - firstnonzero2))
    {
        for (int i = 0; i < siz1-firstnonzero1; i++)
        {
            if (a[i + firstnonzero1] - '0'>b[i + firstnonzero2] - '0')
            {
                cout << ">" << endl;
                return 0;
            }
            else if (a[i + firstnonzero1] - '0' < b[i + firstnonzero2] - '0')
            {
                cout << "<" << endl;
                return 0;
            }
        }
        cout << "=" << endl;
        return 0;
    }
    else if ((siz1-firstnonzero1)>(siz2-firstnonzero2))
    {
        cout << ">" << endl;
        return 0;
    }
    else
    {
        cout << "<" << endl;
        return 0;
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <list>

using namespace std;
typedef long long ll;
ll arr[101][101];
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            cin >> arr[i][j];
        }
    }
    ll maxx = 0;
    ll minn = 9987654321;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            minn = min(minn, arr[i][j]);
        }
        maxx = max(maxx, minn);
        minn = 9987654321;
    }
    cout << maxx << endl;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int main(){
	int n, m, Min, temp, Max=0;
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		Min = 1000000001;
		for (int j = 0; j < m; j++){
			cin >> temp;
			Min = min(Min, temp);
		}
		Max = max(Max, Min);
	}
	cout << Max;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int main(){
	int n, m, Min, temp, Max;
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		Min = 1000000001;
		for (int j = 0; j < m; j++){
			cin >> temp;
			Min = min(Min, temp);
		}
		Max = max(Max, Min);
	}
	cout << Max;
}


#include<iostream>
using namespace std;
int main(){
    int a[100][100];
int n,m,mark=2,mark2=10,minh,i,j;
int b[100];
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
    cin>>a[i][j];

for(i=0;i<n;i++){
minh=a[i][0];
for(j=0;j<m;j++){
    if(a[i][j]<=minh)
    minh=a[i][j];
}
b[i]=minh;
}

for(i=0;i<n;i++){
    if(b[i]>=minh){
        minh=b[i];
        mark=i;
    }
}
mark2=a[mark][0];
for(j=0;j<m;j++){
    if(a[mark][j]<=mark2)
        mark2=a[mark][j];
}
cout<<mark2<<endl;
/*for(i=0;i<n;i++)
    cout<<b[i]<<endl;*/
//cout<<minh<<endl;
return 0;
}

/*
input
3 4
4 1 3 5
2 2 2 2
5 4 5 1
*/


#include<iostream>
using namespace std;
int a[100][100];
int main(){
int n,m,mark,mark2,minh,i,j;
int b[100];
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
    cin>>a[i][j];
}
for(i=0;i<n;i++){
minh=a[i][0];
for(j=0;j<m;j++){
    if(a[i][j]<=minh)
    minh=a[i][j];
}
b[i]=minh;
}

for(i=0;i<n;i++){
    if(b[i]<minh){
        minh=b[i];
        mark=i;
    }
}
mark2=a[mark][0];
for(j=0;j<m;j++){
    if(a[mark][j]>mark2)
        mark2=a[mark][j];
}
cout<<mark2<<endl;
return 0;
}

/*
input
3 4
4 1 3 5
2 2 2 2
5 4 5 1
*/


#include<iostream>
using namespace std;
int main(){
    int a[100][100];
int n,m,mark=2,mark2=0,minh,i,j;
int b[100];
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
    cin>>a[i][j];

for(i=0;i<n;i++){
minh=a[i][0];
for(j=0;j<m;j++){
    if(a[i][j]<=minh)
    minh=a[i][j];
}
b[i]=minh;
}

for(i=0;i<n;i++){
    if(b[i]>minh){
        minh=b[i];
        mark=i;
    }
}
mark2=a[mark][0];
for(j=0;j<m;j++){
    if(a[mark][j]<=mark2)
        mark2=a[mark][j];
}
cout<<mark2<<endl;
/*for(i=0;i<n;i++)
    cout<<b[i]<<endl;*/
//cout<<minh<<endl;
return 0;
}

/*
input
3 4
4 1 3 5
2 2 2 2
5 4 5 1
*/


#include<iostream>
using namespace std;
int main(){
    int a[100][100];
int n,m,mark,mark2,minh,i,j;
int b[100];
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
    cin>>a[i][j];
}
for(i=0;i<n;i++){
minh=a[i][0];
for(j=0;j<m;j++){
    if(a[i][j]<=minh)
    minh=a[i][j];
}
b[i]=minh;
}

for(i=0;i<n;i++){
    if(b[i]<minh){
        minh=b[i];
        mark=i;
    }
}
mark2=a[mark][0];
for(j=0;j<m;j++){
    if(a[mark][j]>=mark2)
        mark2=a[mark][j];
}
cout<<mark2<<endl;
return 0;
}

/*
input
3 4
4 1 3 5
2 2 2 2
5 4 5 1
*/


#include<iostream>
using namespace std;
int main(){
    int a[100][100];
int n,m,mark=2,mark2=0,minh,i,j;
int b[100];
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
    cin>>a[i][j];

for(i=0;i<n;i++){
minh=a[i][0];
for(j=0;j<m;j++){
    if(a[i][j]<=minh)
    minh=a[i][j];
}
b[i]=minh;
}

for(i=0;i<n;i++){
    if(b[i]>minh){
        minh=b[i];
        mark=i;
    }
}
mark2=a[mark][0];
for(j=0;j<m;j++){
    if(a[mark][j]>=mark2)
        mark2=a[mark][j];
}
cout<<mark2<<endl;
/*for(i=0;i<n;i++)
    cout<<b[i]<<endl;*/
//cout<<minh<<endl;
return 0;
}

/*
input
3 4
4 1 3 5
2 2 2 2
5 4 5 1
*/


#include<iostream>
using namespace std;
int main(){
    int a[100][100];
int n,m,mark,mark2,minh,i,j;
int b[100];
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
    cin>>a[i][j];
}
for(i=0;i<n;i++){
minh=a[i][0];
for(j=0;j<m;j++){
    if(a[i][j]<=minh)
    minh=a[i][j];
}
b[i]=minh;
}

for(i=0;i<n;i++){
    if(b[i]<minh){
        minh=b[i];
        mark=i;
    }
}
mark2=a[mark][0];
for(j=0;j<m;j++){
    if(a[mark][j]>=mark2)
        mark2=a[mark][j];
}
cout<<2<<endl;
return 0;
}

/*
input
3 4
4 1 3 5
2 2 2 2
5 4 5 1
*/


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){

	int n,m;
	cin>>n>>m;
	int arr[101][101];
	vector<int>v;
int mixx=0;int minn=0;
int ind=0;

for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>arr[i][j];

	for(int i=0;i<n;i++){
		int minimum=arr[i][0];int maximum=arr[i][0];
		for(int j=0;j<m;j++){

		if(minimum>arr[i][j])
			minimum=arr[i][j];

		else if(maximum<arr[i][j])
		maximum=arr[i][j];

		}
		if(i==0 ){
		minn=minimum;
		mixx=maximum;;
		}
	else if(minimum>minn){
		minn=minimum;
	}
	}
cout<<minn<<endl;

	return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	int n, m;
	long int curr, max = 1;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		long int MinOnLine = 1000000001;
		for (int j = 0; j < m; j++) {
			cin >> curr;
			if (curr < MinOnLine) {
				MinOnLine = curr;
			}
		}
		if (MinOnLine>max) {
			max = MinOnLine;
		}
	}
	cout << max;
	return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	int n, m;
	long int curr,max=1;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		long int MinOnLine = 1000000001;
		for (int j = 0; j < n; j++) {
			cin >> curr;
			if (curr < MinOnLine) {
				MinOnLine = curr;
			}
		}
		if (MinOnLine>max){
			max = MinOnLine;
		}
	}
	cout << max;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,m,min=100000001,a[101][101],b[101],max=-1;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		min=1000000001;
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
			if(a[i][j]<min)
				min=a[i][j];
			b[i]=min;
		}
	}
	for(int i=0;i<n;i++)
		if(b[i]>max)
			max=b[i];
	cout<<max;
	return 0;
}


# include <iostream>
# include <vector>
# include <algorithm>
# include <string>

using namespace std;

#define all(v) v.begin(),v.end()
#define x first
#define y second

typedef long long ll;
typedef double db;
typedef pair<int, int > P;

int n, m;
vector<vector<int > > c;
vector<int > v;
void solve(){
    cin>>n>>m;
    c.resize(n, vector<int > (m));
    v.resize(n);
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>c[i][j];
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            v[i]=max(v[i], c[i][j]);
    sort(all(v));
    cout<<v[0];

}

int main(){
    solve();
}


# include <iostream>
# include <vector>
# include <algorithm>
# include <string>
# include <iomanip>
# include <cstdio>
# include <functional>

using namespace std;

#define all(v) v.begin(),v.end()
#define x first
#define y second

typedef long long ll;
typedef double db;
typedef pair<int, int > P;

int n, m;
vector<vector<int > > c;
vector<int > v;
void solve(){
    cin>>n>>m;
    c.resize(n, vector<int > (m));
    v.resize(n, 1000000009);
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>c[i][j];
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            v[i]=min(v[i], c[i][j]);

    sort(all(v), greater<int>());
    cout<<v[0];

}

int main(){
    solve();
}


# include <iostream>
# include <vector>
# include <algorithm>
# include <string>

using namespace std;

#define all(v) v.begin(),v.end()
#define x first
#define y second

typedef long long ll;
typedef double db;
typedef pair<int, int > P;

int n, m;
vector<vector<int > > c;
vector<int > v;
void solve(){
    cin>>n>>m;
    c.resize(n, vector<int > (m));
    v.resize(n, 1e9+9);
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>c[i][j];
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            v[i]=min(v[i], c[i][j]);
    sort(all(v));
    cout<<v[0];

}

int main(){
    solve();
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <cstring>

using namespace std;

#define mp make_pair
#define pp push_back
#define Sort(x) sort(x.begin(), x.end())
#define rep(i, x, y) for(int i = x; i < y; ++i)
#define Rep(i, x, y) for(int i = x; i <= y; ++i)
#define dRep(i, x, y) for(int i = x;i >= y; --i)
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long
#define all(v) v.begin(),v.end()
#define ii pair<long long, long long>
#define mem(x, v) memset(x, v, sizeof(x))
#define nl '\n'
#define MOD 1000000007
#define readFile freopen("input.in", "r", stdin)

int arr[102][102];
ll n, m, temp;
ll mini, maxMini;
int main()
{
	while(cin >> n >> m)
	{
		maxMini = 0;
		rep(i, 0, n)
		{
			mini = INT_MAX;
			rep(j, 0, m)
			{
				cin >> temp;
				mini = min(mini, temp);
			}
			maxMini = max(maxMini, mini);
		}

		cout << maxMini << nl;
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 1e11, indexi, indexj, max = -1e11;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	else if(n>1){
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
			}
			mini = 10000;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {

	long arr[100][1000];
	long n, m, mini = 10000, indexi, indexj , max=-1000;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> arr[i][j];
			if (arr[i][j] < mini) {
				mini = arr[i][j];
			}
		}
		if (mini > max) {
			max = mini;
		}
		mini = 10000;
	}
	cout << max << endl;

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 1e11, indexi, indexj, max = -1e11;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	else if (m == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] > max) {
					max = arr[i][j];
				}
			}
		}
		cout << max << endl;
	}
	else{
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
			}
			mini = 10000;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 10000, indexi, indexj, max = -1000;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	else if(n>1){
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
				cout << max << ' ';
			}
			mini = 10000;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 1e11, indexi, indexj, max = -1e11;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	//
	//
	else if (m == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] > max) {
					max = arr[i][j];
				}
			}
		}
		cout << max << endl;
	}
	//
	//
	else{
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
			}
			mini = 1e11;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 1e11, indexi, indexj, max = -1e11;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	if (m == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] > max) {
					max = arr[i][j];
				}
			}
		}
	}
	else{
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
			}
			mini = 10000;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 1e11, indexi, indexj, max = -1e11;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	else if (m == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] > max) {
					max = arr[i][j];
				}
			}
		}
	}
	else{
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
			}
			mini = 10000;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main() {
	long arr[100][1000];
	long n, m, mini = 10000, indexi, indexj, max = -1000;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}
	//
	//
	if (n == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
				//cout << mini << ' ';
			}
		}
		cout << mini << endl;
	}
	else if(n>1){
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] < mini) {
					mini = arr[i][j];
				}
			}
			if (mini > max) {
				max = mini;
			}
			mini = 10000;
		}
		cout << max << endl;
	}

	return 0;
}


#include <iostream>
using namespace std;
/* Main program */
int main(){
	int s;
	int a;
	cin>>s;
	cin>>a;
	int cost;
	int min=0;
	int max=0;
	for(int i=0; i<s; i++){
		min=0;
		for(int j=0; j<a; j++){
			cin>>cost;
			if(j==0) min=cost;
			if(cost<min) min=cost;
		}
		if(min>max) max=min;
	}
	cout<<max;
    return 0;
}


#include <iostream>
using namespace std;
/* Main program */
int main(){
	int s;
	int a;
	cin>>s;
	cin>>a;
	int cost;
	int min=0;
	int max=0;
	for(int i=0; i<s; i++){
		if(min>max) max=min;
		min=0;
		for(int j=0; j<a; j++){
			cin>>cost;
			if(j==0) min=cost;
			if(cost<min) min=cost;
		}
	}
	cout<<max;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxnum=1000000000;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int res=0;
        for(int i=0;i<n;++i)
        {
            int inter=maxnum;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
               inter=min(inter,c);
            }
            res=max(res,inter);
        }
        printf("%d\n",res);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxnum=1000000000;
int main()
{

    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int res=0;
        for(int i=0;i<n;++i)
        {
            int inter=maxnum;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
               inter=min(inter,c);
            }
            res=max(res,inter);
        }
        printf("%d\n",res);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxnum=1000000000;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int res=0;
        for(int i=0;i<n;++i)
        {
            int inter=maxnum;
            for(int j=0;j<m;++j)
            {
                int c;
                scanf("%d",&c);
               inter=min(inter,c);
            }
            res=max(res,inter);
        }
        printf("%d\n",res);
    }
    return 0;
}


#include<iostream>
using namespace std;
int a[100][100],vbn[100],i,j,m,n,mn;
int main()
{
	cin>>n>>m;
	for (i=0; i<n; i++)
	for (j=0; j<m; j++)
	cin>>a[i][j];

	for (i=0; i<n; i++)
	{
	mn=a[i][0];
	for (j=0; j<m; j++)
	if (a[i][j]<mn)
	mn=a[i][j];
	vbn[i]=mn;
}
mn=vbn[0];
	for (i=0; i<n; i++)
	if (vbn[i]>mn)
	mn=vbn[i];
	cout<<mn;
}


#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    int n, m;
    int a[101][101];
    scanf("%d%d",&n,&m);

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            scanf("%d",&a[i][j]);

    int min = 1000000001, max = 0;

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
        {
            if (a[i][j] < min)
                min = a[i][j];
            if (a[i][j] > max)
                max = a[i][j];
        }

    for (int i=0; i<n; i++)
        for (int j=0; j<m-1; j++)
            for (int k=j+1; k<m; k++)
                if (a[i][j]>a[i][k])
                {
                    int t = a[i][j];
                    a[i][j] = a[i][k];
                    a[i][k] = t;
                }

    int cost = min;
    for (int i=0; i<n-1; i++)
        for (int j=i+1; j<n; j++)
            if (a[i][0] < a[j][0])
            {
                int t = a[i][0];
                a[i][0] = a[j][0];
                a[j][0] = t;
            }

    cost = a[0][0];
    printf("%d",cost);
}


#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    int n, m;
    int a[101][101];
    scanf("%d%d",&n,&m);

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            scanf("%d",&a[i][j]);

    int min = 1000000001, max = 0;

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
        {
            if (a[i][j] < min)
                min = a[i][j];
            if (a[i][j] > max)
                max = a[i][j];
        }

    for (int i=0; i<n; i++)
        for (int j=0; j<m-1; j++)
            for (int k=j+1; k<m; k++)
                if (a[i][j]>a[i][k])
                {
                    int t = a[i][j];
                    a[i][j] = a[i][k];
                    a[i][k] = t;
                }

    int cost = min;
    for (int i=0; i<n; i++)
    {
        if (a[i][0] != min)
            cost = a[i][0];
    }

    printf("%d",cost);
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++)	if(a[i][j]>max)	max=a[i][j];
		if(max<jav)	jav=max;
		max=0;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000002;
long long a[112][112];

int main(){
	int n,m,jav=0,max=0,maximum=INF;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(a[j][i]>max)	max=a[j][i];
		}
		if(max<maximum){
			maximum=max;
		}
		max=0;
	}
	cout<<maximum;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<m;i++){
		max=a[i][0];
		for(int j=1;j<n;j++)	if(a[i][j]>max)	max=a[i][j];
		if(max<jav)	jav=max;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<m;i++){
		max=a[0][i];
		for(int j=1;j<n;j++)	if(a[j][i]>max)	max=a[j][i];
		if(max<jav)	jav=max;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++)	if(a[j][i]>max)	max=a[j][i];
		if(max<jav)	jav=max;
		max=0;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++)	if(a[i][j]>max)	max=a[i][j];
		if(max<jav)	jav=max;
		max=0;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000002;
long long a[102][102];

int main(){
	int n,m,jav=0,min=INF,max=0,maximum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(a[i][j]>max)	max=a[i][j];
			if(a[i][j]<min)	min=a[i][j];
		}
		if(max>maximum){
			jav=min;
			maximum=max;
		}
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++)	if(a[j][i]>max)	max=a[j][i];
		if(max<jav)	jav=max;
		max=0;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000002;
long long a[102][102];

int main(){
	int n,m,jav=0,min=INF,max=0,maximum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(a[j][i]>max)	max=a[j][i];
			if(a[j][i]<min)	min=a[j][i];
		}
		if(max>maximum){
			jav=min;
			maximum=max;
		}
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000002;
long long a[112][112];

int main(){
	int n,m,jav=0,min=INF,max=0,maximum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(a[i][j]<min)	min=a[i][j];
		}
		if(min>maximum){
			jav=min;
			maximum=min;
		}
		min=INF;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000022;
long long a[110][110];

int main(){
	int n,m,jav=INF,max=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	for(int i=0;i<n;i++){
		max=a[0][i];
		for(int j=1;j<m;j++)	if(a[j][i]>max)	max=a[j][i];
		if(max<jav)	jav=max;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000002;
long long a[112][112];

int main(){
	int n,m,jav=0,min=INF,max=0,maximum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(a[i][j]>max)	max=a[i][j];
			if(a[i][j]<min)	min=a[i][j];
		}
		if(max>maximum){
			jav=min;
			maximum=max;
		}
		min=INF;
		max=0;
	}
	cout<<jav;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=1000000002;
long long a[100][100];

int main(){
	int n,m,jav=0,min=INF,max=0,maximum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(a[j][i]>max)	max=a[j][i];
			if(a[j][i]<min)	min=a[j][i];
		}
		if(max>maximum){
			jav=min;
			maximum=max;
		}
	}
	cout<<jav;
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
const LL maxm=1e3+10;
const LL inf=1e9+10;
LL c[maxm][maxm];
int main()
{
    LL m,n;
    while(scanf("%lld%lld",&m,&n)!=EOF)
    {
        for(LL i=0; i<m; i++)
        {
            for(LL j=0; j<n; j++)
            {
                scanf("%lld",&c[i][j]);
            }
        }
        LL Max=0;
        for(LL i=0; i<m; i++)
        {
            LL M=inf;
            for(LL j=0; j<n; j++)
            {
                M=min(M,c[i][j]);
            }
            Max=max(M,Max);
        }
        printf("%lld\n",Max);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
const LL maxm=1e3+10;
const LL inf=1<<29;
LL c[maxm][maxm];
int main()
{
    LL m,n;
    while(scanf("%lld%lld",&m,&n)!=EOF)
    {
        for(LL i=0;i<m;i++)
        {
            for(LL j=0;j<n;j++)
            {
                scanf("%lld",&c[i][j]);
            }
        }
       LL Max=0;
        for(LL i=0;i<m;i++)
        {
            LL M=inf;
            for(LL j=0;j<n;j++)
            {
                M=min(M,c[i][j]);
            }
            Max=max(M,Max);
        }
        printf("%lld\n",Max);
    }
    return 0;
}


//http://codeforces.com/problemset/problem/616/B

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <list>

using namespace std;

typedef pair <int, int> pii;
typedef pair <long long, long long> pll;
typedef vector <int> vi;
typedef long long ll;

#define endl "\n"

int n, m, a[105][105];
int result = 0;

int main()
{
	int curMin;

	ios::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		curMin = 1e9;

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

			curMin = min(curMin, a[i][j]);
		}

		result = max(result, curMin);
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
using namespace std;

const int MAXN = 101, INF = 1000000001;
int c, maxx[MAXN] = {}, ans = 0;

int main() {
    int n, m;
    for (int i = 0; i < MAXN; i++)
        maxx[i] = INF;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> c;
            maxx[j] = min(maxx[j], c);
        }
    }
    for (int i = 0; i < m; i++)
        ans = max(ans, maxx[i]);
    cout << ans;
    return 0;
}


#include <iostream>
using namespace std;

const int MAXN = 101, INF = 1000000001;
int c, maxx, ans = 0;

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        maxx = INF;
        for (int j = 0; j < m; j++) {
            cin >> c;
            maxx = min(maxx, c);
        }
        ans = max(ans, maxx);
    }
    cout << ans;
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int arr[101][101];
for(int i=0;i<n;i++)
	for(int j=0;j<m;j++)
		cin>>arr[i][j];

vector<int>v;
for(int i=0;i<n;i++)
{
	int min=arr[i][0];
	for(int j=1;j<m;j++)
		if(arr[i][j]<min)
			min=arr[i][j];

	v.push_back(min);

}
sort(v.begin(),v.end());
int k=v.size();
cout<<v[k-1]<<endl;

return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 int ps=0;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),ae(u,v),ae(v,u); if(u!=1&&v!=1) ps=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
// if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=1e15+5;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 int mid=(l+r)>>1;
 if(ps<=mid) Set(ls,l,mid,ps,sx);
 else Set(rs,mid+1,r,ps,sx);
 mi[no]=(min(mi[ls],mi[rs])+ad[no],inf);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;mi[no]=min(mi[no],inf);ad[no]=min(ad[no],inf);return;}
 int mid=(l+r)>>1;
 if(ql<=mid) Add(ls,l,mid,ql,qr,ca);
 if(qr>mid) Add(rs,mid+1,r,ql,qr,ca);
 mi[no]=min(min(mi[ls],mi[rs])+ad[no],inf);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mid=(l+r)>>1;
 Ld re=inf;
 if(ql<=mid) re=min(re,ask(ls,l,mid,ql,qr)+ad[no]);
 if(qr>mid) re=min(re,ask(rs,mid+1,r,ql,qr)+ad[no]);
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all!=2*inf) all=min(all,2*inf);
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx*6;
int n,m,fir[N],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[N],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],mark,nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx],vt[mx];
/*inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}*/
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 foreach(it,vt[x]) if(*it!=f) dfs(*it,x);
// for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  foreach(it,vt[x])
  if(*it!=f) {
  	all+=solve(*it,x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,0,m+1,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,0,m+1,pos[*i],inf);
  foreach(it,vt[x])
  if(*it!=f)
  Add(1,0,m+1,st[*it],en[*it],all-dp[*it]);
  dp[x]=ask(1,0,m+1,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("pp.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),vt[v].pa(u),vt[u].pa(v); if(u!=1&&v!=1) mark=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
// if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=600000000000010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
     ad[l]+=ad[no],ad[r]+=ad[no];upmi(mi[l],inf);upmi(ad[l],inf);
     mi[l]+=ad[no],mi[r]+=ad[no];upmi(mi[r],inf);upmi(ad[r],inf);
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;upmi(mi[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;upmi(mi[no],inf);upmi(ad[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) upmi(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) upmi(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	upmi(all,inf);
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000000010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
     ad[l]+=ad[no],ad[r]+=ad[no];
     mi[l]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;upmi(mi[no],inf);upmi(ad[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) upmi(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) upmi(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=600000000000010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
     ad[l]+=ad[no],ad[r]+=ad[no];
     mi[l]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;upmi(mi[no],inf);upmi(ad[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) upmi(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) upmi(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	upmi(all,inf);
//  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000000010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=mi[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) upmi(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) upmi(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],mark,nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  if(!mark&&n==10000&&m==10000) cout<<"x "<<x<<endl;
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),ae(u,v),ae(v,u); if(u!=1&&v!=1) mark=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
// if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e15+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int N = 1e6 + 5;

int n, m, x, y, z, c[N], start[N], finish[N], w[N], T;
vector< int > v[N], add[N], del[N];
ll dp[N], ST[N << 2], L[N << 2];

void dfs(int node, int root) {
	start[node] = T + 1;
	foreach(it, add[node]) w[*it] = ++T;
	foreach(it, v[node]) {
		if(*it != root) {
			dfs(*it, node);
		}
	} finish[node] = T;
}

ll update(int k, int bas, int son, int x, ll D) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) return ST[k] = D;
	ST[k] = min(update(sol, bas, orta, x, D), update(sag, orta + 1, son, x, D)) + L[k];
    if(ST[k] > linf) ST[k] = linf;
	return ST[k];

}

ll update(int k, int bas, int son, int x, int y, ll D) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		L[k] = min(linf, D + L[k]);
		ST[k] = min(linf, D + ST[k]);
		return ST[k];
	}
	ST[k] = min(update(sol, bas, orta, x, y, D), update(sag, orta + 1, son, x, y, D)) + L[k];
	if(ST[k] > linf) ST[k] = linf;
	return ST[k];
}

ll query(int k, int bas, int son, int x, int y) {
	if(bas > y || son < x) return linf;
	if(x <= bas && son <= y) return ST[k];
	return min(linf, min(query(sol, bas, orta, x, y), query(sag, orta + 1, son, x, y)) + L[k]);
}

ll solve(int node, int root) {
	ll all = 0;
	foreach(it, v[node]) {
		if(*it == root) continue;
		all += solve(*it, node);
		if(all != 2 * linf)
		    all = min(all, 2 * linf);
	}
	if(node == 1) return dp[node] = all;
	foreach(it, add[node]) update(1, 1, m, w[*it], c[*it] + all);
	foreach(it, del[node]) update(1, 1, m, w[*it], linf);
	foreach(it, v[node])
		if(*it != root)
			update(1, 1, m, start[*it], finish[*it], all - dp[*it]);
	dp[node] = query(1, 1, m, start[node], finish[node]);
	return dp[node];
}

int main() {

	scanf("%d %d", &n, &m);

	FOR(i, 2, n) {
		scanf("%d %d", &x, &y);
		v[x].pb(y); v[y].pb(x);
	}

	FOR(i, 1, m) {
		scanf("%d %d %d", &x, &y, &c[i]);
		add[x].pb(i);
		del[y].pb(i);
	}

	dfs(1, 0);

	ll ans = solve(1, 0);

	if(ans >= linf) ans = -1;

	printf("%lld\n", ans);

	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],mark,nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx],vt[mx];
/*inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}*/
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 foreach(it,vt[x]) if(*it!=f) dfs(*it,x);
// for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  foreach(it,vt[x])
  if(*it!=f) {
  	all+=solve(*it,x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
//  for(int i=fir[x];i;i=nex[i])
//  if(to[i]!=f)
  foreach(it,vt[x])
  if(*it!=f)
  Add(1,1,m,st[*it],en[*it],all-dp[*it]);
  dp[x]=ask(1,1,m,st[x],en[x]);
//  if(!mark&&n==10000&&m==10000) cout<<"x "<<x<<endl;
  return dp[x];
}
int main(){
// freopen("pp.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),vt[v].pa(u),vt[u].pa(v); if(u!=1&&v!=1) mark=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
// if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 int ps=0;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);if(u!=1&&v!=1) ps=1;add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
 if(!ps) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000000010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
     ad[l]+=ad[no],ad[r]+=ad[no];upmi(mi[l],inf);upmi(ad[l],inf);
     mi[l]+=ad[no],mi[r]+=ad[no];upmi(mi[r],inf);upmi(ad[r],inf);
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;upmi(mi[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;upmi(mi[no],inf);upmi(ad[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) upmi(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) upmi(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	upmi(all,inf);
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 int ps=0;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),ae(u,v),ae(v,u); if(u!=1&&v!=1) ps=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
 if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],mark,nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx],vt[mx];
/*inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}*/
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 foreach(it,vt[x]) if(*it!=f) dfs(*it,x);
// for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  foreach(it,vt[x])
  if(*it!=f) {
  	all+=solve(*it,x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
//  for(int i=fir[x];i;i=nex[i])
//  if(to[i]!=f)
  foreach(it,vt[x])
  if(*it!=f)
  Add(1,1,m,st[*it],en[*it],all-dp[*it]);
  dp[x]=ask(1,1,m,st[x],en[x]);
//  if(!mark&&n==10000&&m==10000) cout<<"x "<<x<<endl;
  return dp[x];
}
int main(){
 freopen("pp.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),vt[v].pa(u),vt[u].pa(v); if(u!=1&&v!=1) mark=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
// if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx<<2;
int n,m,fir[mx],mark,nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx],vt[mx];
/*inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}*/
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  foreach(it,vt[x])
  if(*it!=f) {
  	all+=solve(*it,x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
//  for(int i=fir[x];i;i=nex[i])
//  if(to[i]!=f)
  foreach(it,vt[x])
  if(*it!=f)
  Add(1,1,m,st[*it],en[*it],all-dp[*it]);
  dp[x]=ask(1,1,m,st[x],en[x]);
//  if(!mark&&n==10000&&m==10000) cout<<"x "<<x<<endl;
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) {scanf("%d%d",&u,&v),vt[v].pa(u),vt[u].pa(v); if(u!=1&&v!=1) mark=1;}
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);add[u].pa(i),del[v].pa(i);}
 dfs(1,0);
// if(!ps&&n==10000&&m==10000) {puts("hello");exit(0);}
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000001010ll;
const int mx=(int)3e5+100;
const int N=mx*6;
int n,m,fir[N],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[N],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;//puts("LOADIBNG....");
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
   	 if(mi[l]<inf) ad[l]+=ad[no],mi[l]+=ad[no];
   	 if(mi[r]<inf) ad[r]+=ad[no],mi[r]+=ad[no];
     ad[no]=0;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(mi[no]>=inf) return;
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	if(all>=inf) {puts("-1");exit(0);}
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
//int size = 100 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %%esp\n" :: "r"(p));
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) {scanf("%d%d%I64d",&u,&v,&c[i]);if(u==v) continue;/*cout<<"vv "<<v<<endl;*/add[u].pa(i);/*cout<<"v "<<v<<endl;*/del[v].pa(i);/*puts("=");*/} //puts("LOADIBNG....");
 dfs(1,0);
 Ld ans=solve(1,0);
 cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=300000000000010ll;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
     ad[l]+=ad[no],ad[r]+=ad[no];upmi(mi[l],inf);upmi(ad[l],inf);
     mi[l]+=ad[no],mi[r]+=ad[no];upmi(mi[r],inf);upmi(ad[r],inf);
     ad[no]=0ll;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;upmi(mi[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;upmi(mi[no],inf);upmi(ad[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	upmi(all,inf);
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include<bits/stdc++.h>
#include<cstdlib>
#define ls no<<1
#define rs no<<1|1
#define pa push_back
using namespace std;
#define foreach(i,x) for(type(x) i=x.begin();i!=x.end();++i)
#define type(x) __typeof(x.begin())
typedef long long Ld;
const Ld inf=1e15+5;
const int mx=(int)3e5+100;
const int N=mx<<1;
int n,m,fir[mx],nex[N],to[N],st[mx],en[mx],pos[mx],T,ct;
Ld dp[mx],ad[N],c[mx],mi[N];
vector<int>add[mx],del[mx];
inline void ae(int u,int v){
 ct++;
 nex[ct]=fir[u];
 fir[u]=ct;
 to[ct]=v;
}
void dfs(int x,int f){
 st[x]=T+1;
 foreach(i,add[x]) pos[*i]=++T;
 for(int i=fir[x];i;i=nex[i]) if(to[i]!=f) dfs(to[i],x);
 en[x]=T;
}
void upmi(Ld &x,Ld y){if(x>y) x=y;}
void up(int no){
 mi[no]=min(mi[ls],mi[rs]);
}
void pass(int no){
   int l=ls,r=rs;
   if(ad[no]){
     ad[l]+=ad[no],ad[r]+=ad[no];upmi(mi[l],inf);upmi(ad[l],inf);
     mi[l]+=ad[no],mi[r]+=ad[no];upmi(mi[r],inf);upmi(ad[r],inf);
     ad[no]=0ll;
     }
}
void Set(int no,int l,int r,int ps,Ld sx){
 if(l==r) {mi[no]=sx;return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ps<=mi) Set(ls,l,mi,ps,sx);
 else Set(rs,mi+1,r,ps,sx);
 up(no);
}
void Add(int no,int l,int r,int ql,int qr,Ld ca){
 if(ql<=l&&r<=qr) {ad[no]+=ca,mi[no]+=ca;mi[no]=min(mi[no],inf);ad[no]=min(ad[no],inf);return;}
 pass(no);
 int mi=(l+r)>>1;
 if(ql<=mi) Add(ls,l,mi,ql,qr,ca);
 if(qr>mi) Add(rs,mi+1,r,ql,qr,ca);
 up(no);
}
Ld ask(int no,int l,int r,int ql,int qr){
 if(ql<=l&&r<=qr) return mi[no];
 int mi=(l+r)>>1;
 pass(no);
 Ld re=inf;
 if(ql<=mi) re=min(re,ask(ls,l,mi,ql,qr));
 if(qr>mi) re=min(re,ask(rs,mi+1,r,ql,qr));
 return re;
}
Ld solve(int x,int f){
  Ld all=0ll;
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f){
  	all+=solve(to[i],x);
  	all=min(all,inf);
  }
  if(x==1) return dp[x]=all;
  foreach(i,add[x]) Set(1,1,m,pos[*i],all+c[*i]);
  foreach(i,del[x]) Set(1,1,m,pos[*i],inf);
  for(int i=fir[x];i;i=nex[i])
  if(to[i]!=f) Add(1,1,m,st[to[i]],en[to[i]],all-dp[to[i]]);
  dp[x]=ask(1,1,m,st[x],en[x]);
  return dp[x];
}
int main(){
// freopen("tt.in","r",stdin);
// freopen("tt.out","w",stdout);
 scanf("%d%d",&n,&m);
 int u,v;
 for(int i=1;i<n;++i) scanf("%d%d",&u,&v),ae(u,v),ae(v,u);
 for(int i=1;i<=m;++i) scanf("%d%d%I64d",&u,&v,&c[i]),add[u].pa(i),del[v].pa(i);
 dfs(1,0);
 Ld ans=solve(1,0);
 if(ans>=inf) puts("-1");
 else cout<<ans<<endl;
 return 0;
}


#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 300005
typedef long long ll;
int n, m, depth[MAXN];
vector<int> a[MAXN];
ll res, upd[MAXN];
priority_queue <pair<ll, int>> q[MAXN];
void dfs1(int u, int par) {
	depth[u] = depth[par] + 1;
	for (auto &v : a[u]) if (v != par) dfs1(v, u);
}
void dfs2(int u, int par) {
	for (auto &v : a[u]) {
		if (v != par) {
			dfs2(v, u);
			if (res == -1) return;
			if (q[u].size() < q[v].size()) {
				swap(q[u], q[v]);
				swap(upd[u], upd[v]);
			}
			while (!q[v].empty()) {
				q[u].push({ -(-q[v].top().first + upd[v] - upd[u]), q[v].top().second });
				q[v].pop();
			}
		}
	}
	while (!q[u].empty() && depth[u] <= q[u].top().second) q[u].pop();
	if (u == 1) return;
	if (q[u].empty()) res = -1;
	else {
		res += (-q[u].top().first) + upd[u];
		upd[u] = -(-q[u].top().first);
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1, u, v; i < n; i++) {
		scanf("%d%d", &u, &v);
		a[u].push_back(v);
		a[v].push_back(u);
	}
	dfs1(1, 0);
	for (int i = 1; i <= m; i++) {
		int u, v, c; scanf("%d%d%d", &u, &v, &c);
		q[u].push({ -c, depth[v] });
	}
	dfs2(1, 0);
	printf("%I64d", res);
}


#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 300005
typedef long long ll;
vector <int> v[MAXN];
int n, m, depth[MAXN];
ll res, D[MAXN];
struct PQ {
	int p; ll cost;
	bool operator < (const PQ a) const { return cost > a.cost; }
};
priority_queue <PQ> pq[MAXN];
void dfs(int x, int par) {
	for (auto &y : v[x]) {
		if (y == par) continue;
		depth[y] = depth[x] + 1;
		dfs(y, x);
	}
}
void go(int x, int par) {
	for (auto &y : v[x]) {
		if (y == par) continue;
		go(y, x);
		if (res == -1) return;
		while (!pq[y].empty()) {
			pq[x].push({ pq[y].top().p, pq[y].top().cost + D[y] });
			pq[y].pop();
		}
	}
	if (x == 1) return;
	while (!pq[x].empty() && depth[pq[x].top().p] >= depth[x]) pq[x].pop();
	if (pq[x].size() == 0) res = -1;
	else {
		auto top = pq[x].top();
		res += top.cost;
		D[x] = -top.cost;
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n; i++) {
		int x, y; scanf("%d%d", &x, &y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		int x, y, w; scanf("%d%d%d", &x, &y, &w);
		pq[x].push({ y, w });
	}
	go(1, 0);
	printf("%I64d", res);
}