#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int a[101],b,c,c1,c2,d,x,y,n,i,k,m;
int main(){
cin>>n>>m;
    for(i=0; i<m; i++){
        cin>>x>>y>>c;
        a[x]=a[x]-c;
        a[y]=a[y]+c;
    }
    for (i=0; i<n; i++)
    if (a[i]>0) x=a[i]+a[i];

    cout<<x<<endl;

}


#include<iostream>
#include<cstdio>

int main()
{
    int  n,m;
    while(scanf("%d %d",&n,&m) != EOF)
    {
       int a[105] = {0},b[105] = {0};
        int i,j,maxn = 0;
        int k,p,q;
        if(m == 0)
            printf("%d\n",maxn);
        else
        {
            for(i = 1;i <= m;i++)
            {
                scanf("%d%d%d",&k,&p,&q);
                a[k] += q;
                b[p] += q;
            }
        for(i = 1;i <= n;i++)
        {
            if(b[i] >= a[i])
            {
                b[i] = b[i] - a[i];
                a[i] = 0;
            }
            else
                a[i] = a[i] - b[i];
        }
        for(i = 1;i <= n;i++)
        {
            maxn += a[i];
        }
        printf("%d\n",maxn);
        }
    }
    return 0;
}


#include<iostream>
using namespace std;
int a[101],n,m,x,y,j,sum;
int main(){
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>x>>y>>j;
		a[x]-=j;
		a[y]+=j;
	}
	for(int i=1;i<=n;i++)if(a[i]>0)sum+=a[i];
	cout<<sum;
}


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

#define LL long long
int n, m, a, b, c;
LL in[101], out[101];
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0 ; i < m ; i++ ) {
		scanf("%d %d %d", &a, &b, &c );
		out[a] += c;
		in[b] += c;
	}
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		if (in[i] >= out[i])
			in[i] -= out[i];
		ans += in[i];
	}
	cout << ans << endl;
	return 0;
}


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

#define LL long long
int n, m, a, b, c;
LL in[101], out[101];
bool vis[101];
vector<int> G[101];
vector<int> topo;
void dfs( int cur ) {

	vis[cur] = true;
	for (int i = 0; i < G[cur].size(); i++) {
		int to = G[cur][i];
		if ( not vis[to])
			dfs(to);
	}
	topo.push_back(cur);
}
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0 ; i < m ; i++ ) {
		scanf("%d %d %d", &a, &b, &c );
		out[a] += c;
		in[b] += c;
		G[a].push_back(b);
	}
	for (int i = 1; i <= n; i++)
		if (in[i] == 0)
			dfs(i);
	reverse( topo.begin(), topo.end());
	LL ans = 0;
	for (int i = 0; i < topo.size(); i++) {
		int cur = topo[i];
		if (in[cur] >= out[cur])
			in[cur] -= out[cur];
		else {
			if (i + 1 < topo.size())
				in[ topo[i + 1] ] -= in[cur];
		}
		ans += in[cur];
	}
	cout << ans << endl;
	return 0;
}


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

#define LL long long
int n, m, a, b, c;
int in[101], out[101];
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0 ; i < m ; i++ ) {
		scanf("%d %d %d", &a, &b, &c );
		out[a] += c;
		in[b] += c;
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (in[i] >= out[i])
			in[i] -= out[i];
		ans += in[i];
	}
	printf("%d\n", ans );
	return 0;
}


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

#define LL long long
int n, m, a, b, c;
LL in[101], out[101];
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0 ; i < m ; i++ ) {
		scanf("%d %d %d", &a, &b, &c );
		out[a] += c;
		in[b] += c;
	}
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		if (in[i] - out[i] > 0 )
			ans += in[i] - out[i];
	}
	cout << ans << endl;
	return 0;
}


#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <string.h>
#include <assert.h>
#include <time.h>

#define LOCAL_TEST true
#define SZ(x) ((int)x.size())
#define clrall(name,val) memset(name,(val),sizeof(name));
#define clrint(name,val,s) memset(name,(val),sizeof(int)*s);
#define Assign(name,val) name.assign(val+1,vector<int>())
#define EPS .000000001
#define ll long long
#define ull long long unsigned
#define SF scanf
#define PF printf
#define psb(b) push_back((b))
#define ppb() pop_back()
#define oo (1<<30)
#define mp make_pair
#define fs first
#define sc second
#define rep(var,s,n) for(var=(s);var<(n);(var)++)
#define rvp(var,s,n) for(var=(n-1);var>(s-1);(var)--)
#define read_ freopen("input.txt","r",stdin)
#define write_ freopen("outputtest.txt","w",stdout)

using namespace std;

template<class T>inline T Max(T a,T b){return a>b?a:b;}
template<class T>inline T Min(T a,T b){return a>b?b:a;}
template<class T>inline T _GCD(T a,T b){if(b==0) return a;return _GCD(b,a%b);}
template<class T>inline T _LCM(T a,T b){T g=_GCD(a,b);return ((a/g)*b);}
template<class T>inline T big_mod(T a,T p,T MOD){if(p==0) return 1;T ret=big_mod(a,p/2,MOD);ret=(ret*ret)%MOD;if(ret<0) ret+=MOD;if(p%2) ret=(ret*a)%MOD;if(ret<0) ret+=MOD;return ret;}
template<class T>inline T power(T a,T p){if(p==0) return 1;ll ret=power(a,p/2);ret=(ret*ret);if(p%2) ret=(ret*a);return ret;}
template<class T0,class T1,class T2>inline T1 string_mod_ll(T0 s,T1 mm,T2 Sz){T1 n=0;for(int i=0;i<Sz;i++){n=n*10+(ll)(s[i]-'0');n%=mm;}return n;}

int compare(string a,string b){if(a==b) return 0;if(SZ(a)==SZ(b)){for(int i=0; i<SZ(a); i++){if(a[i]>b[i]) return 1;else if(a[i]<b[i]) return -1;}return -1;}if(SZ(a)>SZ(b)) return 1;return -1;}
string string_add(string b,string a){if(SZ(a)>SZ(b)) swap(a,b);a=string((SZ(b)-SZ(a)),'0')+a;int carry=0,add;string res;for(int i=SZ(a)-1; i>-1; i--){add=(int)(a[i]-'0')+(int)(b[i]-'0')+carry;carry=add/10;res+=(add%10)+'0';}if(carry) res+=carry+'0';reverse(res.begin(),res.end());return res;}
string string_mul(string a,string b){if(SZ(a)>SZ(b)) swap(a,b);string res,tres;int l=0,carry,mul;for(int i=SZ(a)-1; i>-1; i--){tres=string(l,'0');carry=0;for(int j=SZ(b)-1; j>-1; j--){mul=(int)(a[i]-'0')*(int)(b[j]-'0')+carry;carry=mul/10; tres+=(mul%10)+'0'; }if(carry) tres+=carry+'0';reverse(tres.begin(),tres.end());res=string_add(tres,res);l++;}return res;}
void cut_leading_zero(string &res){int i;for(i=0; i<SZ(res); i++){if(res[i]!='0') break;}res=res.substr(i);if(!SZ(res)) res="0";return;}

///pass a vector to a funtion: funtion (vector <data type> &vector name);
///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
///int rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
///int cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

using namespace std;

int val[111];

int main()
{
    int n,m,a,b,c;
    while(cin>>n>>m)
    {
        clrall(val,0);
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            val[a]+=c;
            val[b]-=c;
        }
        int pos=0,neg=0;
        for(int i=0;i<n;i++)
        if(val[i]>0) pos+=val[i];
        else neg+=val[i];
        cout<<-neg<<endl;
    }
    return 0;
}


#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <string.h>
#include <assert.h>
#include <time.h>

#define LOCAL_TEST true
#define SZ(x) ((int)x.size())
#define clrall(name,val) memset(name,(val),sizeof(name));
#define clrint(name,val,s) memset(name,(val),sizeof(int)*s);
#define Assign(name,val) name.assign(val+1,vector<int>())
#define EPS .000000001
#define ll long long
#define ull long long unsigned
#define SF scanf
#define PF printf
#define psb(b) push_back((b))
#define ppb() pop_back()
#define oo (1<<30)
#define mp make_pair
#define fs first
#define sc second
#define rep(var,s,n) for(var=(s);var<(n);(var)++)
#define rvp(var,s,n) for(var=(n-1);var>(s-1);(var)--)
#define read_ freopen("input.txt","r",stdin)
#define write_ freopen("outputtest.txt","w",stdout)

using namespace std;

template<class T>inline T Max(T a,T b){return a>b?a:b;}
template<class T>inline T Min(T a,T b){return a>b?b:a;}
template<class T>inline T _GCD(T a,T b){if(b==0) return a;return _GCD(b,a%b);}
template<class T>inline T _LCM(T a,T b){T g=_GCD(a,b);return ((a/g)*b);}
template<class T>inline T big_mod(T a,T p,T MOD){if(p==0) return 1;T ret=big_mod(a,p/2,MOD);ret=(ret*ret)%MOD;if(ret<0) ret+=MOD;if(p%2) ret=(ret*a)%MOD;if(ret<0) ret+=MOD;return ret;}
template<class T>inline T power(T a,T p){if(p==0) return 1;ll ret=power(a,p/2);ret=(ret*ret);if(p%2) ret=(ret*a);return ret;}
template<class T0,class T1,class T2>inline T1 string_mod_ll(T0 s,T1 mm,T2 Sz){T1 n=0;for(int i=0;i<Sz;i++){n=n*10+(ll)(s[i]-'0');n%=mm;}return n;}

int compare(string a,string b){if(a==b) return 0;if(SZ(a)==SZ(b)){for(int i=0; i<SZ(a); i++){if(a[i]>b[i]) return 1;else if(a[i]<b[i]) return -1;}return -1;}if(SZ(a)>SZ(b)) return 1;return -1;}
string string_add(string b,string a){if(SZ(a)>SZ(b)) swap(a,b);a=string((SZ(b)-SZ(a)),'0')+a;int carry=0,add;string res;for(int i=SZ(a)-1; i>-1; i--){add=(int)(a[i]-'0')+(int)(b[i]-'0')+carry;carry=add/10;res+=(add%10)+'0';}if(carry) res+=carry+'0';reverse(res.begin(),res.end());return res;}
string string_mul(string a,string b){if(SZ(a)>SZ(b)) swap(a,b);string res,tres;int l=0,carry,mul;for(int i=SZ(a)-1; i>-1; i--){tres=string(l,'0');carry=0;for(int j=SZ(b)-1; j>-1; j--){mul=(int)(a[i]-'0')*(int)(b[j]-'0')+carry;carry=mul/10; tres+=(mul%10)+'0'; }if(carry) tres+=carry+'0';reverse(tres.begin(),tres.end());res=string_add(tres,res);l++;}return res;}
void cut_leading_zero(string &res){int i;for(i=0; i<SZ(res); i++){if(res[i]!='0') break;}res=res.substr(i);if(!SZ(res)) res="0";return;}

///pass a vector to a funtion: funtion (vector <data type> &vector name);
///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
///int rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
///int cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

using namespace std;

int val[111];

int main()
{
    int n,m,a,b,c;
    while(cin>>n>>m)
    {
        clrall(val,0);
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            val[a]+=c;
            val[b]-=c;
        }
        int pos=0,neg=0;
        for(int i=0;i<n;i++)
        if(val[i]>0) pos+=val[i];
        else neg+=val[i];
        cout<<pos<<endl;
    }
    return 0;
}


#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <string.h>
#include <assert.h>
#include <time.h>

#define LOCAL_TEST true
#define SZ(x) ((int)x.size())
#define clrall(name,val) memset(name,(val),sizeof(name));
#define clrint(name,val,s) memset(name,(val),sizeof(int)*s);
#define Assign(name,val) name.assign(val+1,vector<int>())
#define EPS .000000001
#define ll long long
#define ull long long unsigned
#define SF scanf
#define PF printf
#define psb(b) push_back((b))
#define ppb() pop_back()
#define oo (1<<30)
#define mp make_pair
#define fs first
#define sc second
#define rep(var,s,n) for(var=(s);var<(n);(var)++)
#define rvp(var,s,n) for(var=(n-1);var>(s-1);(var)--)
#define read_ freopen("input.txt","r",stdin)
#define write_ freopen("outputtest.txt","w",stdout)

using namespace std;

template<class T>inline T Max(T a,T b){return a>b?a:b;}
template<class T>inline T Min(T a,T b){return a>b?b:a;}
template<class T>inline T _GCD(T a,T b){if(b==0) return a;return _GCD(b,a%b);}
template<class T>inline T _LCM(T a,T b){T g=_GCD(a,b);return ((a/g)*b);}
template<class T>inline T big_mod(T a,T p,T MOD){if(p==0) return 1;T ret=big_mod(a,p/2,MOD);ret=(ret*ret)%MOD;if(ret<0) ret+=MOD;if(p%2) ret=(ret*a)%MOD;if(ret<0) ret+=MOD;return ret;}
template<class T>inline T power(T a,T p){if(p==0) return 1;ll ret=power(a,p/2);ret=(ret*ret);if(p%2) ret=(ret*a);return ret;}
template<class T0,class T1,class T2>inline T1 string_mod_ll(T0 s,T1 mm,T2 Sz){T1 n=0;for(int i=0;i<Sz;i++){n=n*10+(ll)(s[i]-'0');n%=mm;}return n;}

int compare(string a,string b){if(a==b) return 0;if(SZ(a)==SZ(b)){for(int i=0; i<SZ(a); i++){if(a[i]>b[i]) return 1;else if(a[i]<b[i]) return -1;}return -1;}if(SZ(a)>SZ(b)) return 1;return -1;}
string string_add(string b,string a){if(SZ(a)>SZ(b)) swap(a,b);a=string((SZ(b)-SZ(a)),'0')+a;int carry=0,add;string res;for(int i=SZ(a)-1; i>-1; i--){add=(int)(a[i]-'0')+(int)(b[i]-'0')+carry;carry=add/10;res+=(add%10)+'0';}if(carry) res+=carry+'0';reverse(res.begin(),res.end());return res;}
string string_mul(string a,string b){if(SZ(a)>SZ(b)) swap(a,b);string res,tres;int l=0,carry,mul;for(int i=SZ(a)-1; i>-1; i--){tres=string(l,'0');carry=0;for(int j=SZ(b)-1; j>-1; j--){mul=(int)(a[i]-'0')*(int)(b[j]-'0')+carry;carry=mul/10; tres+=(mul%10)+'0'; }if(carry) tres+=carry+'0';reverse(tres.begin(),tres.end());res=string_add(tres,res);l++;}return res;}
void cut_leading_zero(string &res){int i;for(i=0; i<SZ(res); i++){if(res[i]!='0') break;}res=res.substr(i);if(!SZ(res)) res="0";return;}

///pass a vector to a funtion: funtion (vector <data type> &vector name);
///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
///int rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
///int cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

using namespace std;

int val[111];

int main()
{
    int n,m,a,b,c;
    while(cin>>n>>m)
    {
        clrall(val,0);
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            val[a]+=c;
            val[b]-=c;
        }
        int pos=0,neg=0;
        for(int i=1;i<=n;i++)
        if(val[i]>0) pos+=val[i];
        else neg+=val[i];
        cout<<pos<<endl;
    }
    return 0;
}


#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <string.h>
#include <assert.h>
#include <time.h>

#define LOCAL_TEST true
#define SZ(x) ((int)x.size())
#define clrall(name,val) memset(name,(val),sizeof(name));
#define clrint(name,val,s) memset(name,(val),sizeof(int)*s);
#define Assign(name,val) name.assign(val+1,vector<int>())
#define EPS .000000001
#define ll long long
#define ull long long unsigned
#define SF scanf
#define PF printf
#define psb(b) push_back((b))
#define ppb() pop_back()
#define oo (1<<30)
#define mp make_pair
#define fs first
#define sc second
#define rep(var,s,n) for(var=(s);var<(n);(var)++)
#define rvp(var,s,n) for(var=(n-1);var>(s-1);(var)--)
#define read_ freopen("input.txt","r",stdin)
#define write_ freopen("outputtest.txt","w",stdout)

using namespace std;

template<class T>inline T Max(T a,T b){return a>b?a:b;}
template<class T>inline T Min(T a,T b){return a>b?b:a;}
template<class T>inline T _GCD(T a,T b){if(b==0) return a;return _GCD(b,a%b);}
template<class T>inline T _LCM(T a,T b){T g=_GCD(a,b);return ((a/g)*b);}
template<class T>inline T big_mod(T a,T p,T MOD){if(p==0) return 1;T ret=big_mod(a,p/2,MOD);ret=(ret*ret)%MOD;if(ret<0) ret+=MOD;if(p%2) ret=(ret*a)%MOD;if(ret<0) ret+=MOD;return ret;}
template<class T>inline T power(T a,T p){if(p==0) return 1;ll ret=power(a,p/2);ret=(ret*ret);if(p%2) ret=(ret*a);return ret;}
template<class T0,class T1,class T2>inline T1 string_mod_ll(T0 s,T1 mm,T2 Sz){T1 n=0;for(int i=0;i<Sz;i++){n=n*10+(ll)(s[i]-'0');n%=mm;}return n;}

int compare(string a,string b){if(a==b) return 0;if(SZ(a)==SZ(b)){for(int i=0; i<SZ(a); i++){if(a[i]>b[i]) return 1;else if(a[i]<b[i]) return -1;}return -1;}if(SZ(a)>SZ(b)) return 1;return -1;}
string string_add(string b,string a){if(SZ(a)>SZ(b)) swap(a,b);a=string((SZ(b)-SZ(a)),'0')+a;int carry=0,add;string res;for(int i=SZ(a)-1; i>-1; i--){add=(int)(a[i]-'0')+(int)(b[i]-'0')+carry;carry=add/10;res+=(add%10)+'0';}if(carry) res+=carry+'0';reverse(res.begin(),res.end());return res;}
string string_mul(string a,string b){if(SZ(a)>SZ(b)) swap(a,b);string res,tres;int l=0,carry,mul;for(int i=SZ(a)-1; i>-1; i--){tres=string(l,'0');carry=0;for(int j=SZ(b)-1; j>-1; j--){mul=(int)(a[i]-'0')*(int)(b[j]-'0')+carry;carry=mul/10; tres+=(mul%10)+'0'; }if(carry) tres+=carry+'0';reverse(tres.begin(),tres.end());res=string_add(tres,res);l++;}return res;}
void cut_leading_zero(string &res){int i;for(i=0; i<SZ(res); i++){if(res[i]!='0') break;}res=res.substr(i);if(!SZ(res)) res="0";return;}

///pass a vector to a funtion: funtion (vector <data type> &vector name);
///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
///int rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
///int cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

using namespace std;

int val[111][111];

int main()
{
    int n,m,a,b,c;
    while(cin>>n>>m)
    {
        clrall(val,0);
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            val[a][b]=c;
        }
        n++;
        for(int x=0;x<n;x++)
        {
            for(int i=1;i<n;i++)
            {
                for(int j=1;j<n;j++)
                {
                    int tk=val[i][j];
                    if(tk>0)
                    {
                        int tot=0,nd;
                        for(int k=1;k<n;k++)
                        {
                            nd=min(tk,val[j][k]);
                            if(nd)
                            {
//                                cout<<i<<" "<<j<<" "<<k<<endl;
                                tk-=nd;
                                val[j][k]-=nd;
                                val[i][k]+=nd;
                                tot+=nd;
                            }
                            if(tk==0) break;
                        }
                        val[i][j]-=tot;
//                        cout<<i<<" "<<j<<" "<<val[i][j]<<endl;
                    }
                }
            }
        }
        int res=0;
        for(int i=1;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
//                val[i][j]=abs(val[i][j]-val[j][i]);
                val[j][i]=0;
                res+=val[i][j];
//                cout<<val[i][j]<<endl;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}
/*
5 6
1 2 10
2 3 12
3 4 15
4 2 2
2 5 7
5 1 3
*/


#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 <cstring>
#include <cstdlib>
#include <ctime>

#define FOR(i,n) for (int i = 0 ; i< n;i++)
#define FORI(i,s,e) for (int i = s ; i<= e;i++)
#define MP(x,y) make_pair(x,y)
#define PB(x) push_back(x)
#define RALL(x) x.rbegin(),x.rend()
#define SZ(x) x.size()
#define FZ(x) memset(x,0,sizeof(x))

using namespace std;

int matr[129][129];
int main()
{
    int n,m;
    cin >> n >> m;

    for (int i = 0 ; i< m;i++)
    {
        int a , b,c;
        cin >> a >> b>>c;
        matr[a][b]=c;

    }
    for (int k = 0 ; k< n;k++)
    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    if ((matr[i][k] !=0)&&(matr[k][j] !=0))
    {
        int tr = min(matr[i][k],matr[k][j]);
        matr[i][j]+= tr;
        matr[i][k]-=tr;
        matr[k][j]-=tr;

    }
    int su = 0;
    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    su+= matr[i][j];
    cout << su;
}


#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 <cstring>
#include <cstdlib>
#include <ctime>

#define FOR(i,n) for (int i = 0 ; i< n;i++)
#define FORI(i,s,e) for (int i = s ; i<= e;i++)
#define MP(x,y) make_pair(x,y)
#define PB(x) push_back(x)
#define RALL(x) x.rbegin(),x.rend()
#define SZ(x) x.size()
#define FZ(x) memset(x,0,sizeof(x))

using namespace std;

long long matr[129][129];
int main()
{
    int n,m;
    cin >> n >> m;

    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    matr[i][j] = 0;

    for (int i = 0 ; i< m;i++)
    {
        int a , b,c;
        cin >> a >> b>>c;
        matr[a][b]=c;

    }
    for (int te = 0 ; te< n;te++)
    for (int k = 0 ; k< n;k++)
    for (int i = 0 ; i< n;i++)
    if ((matr[i][k] !=0))
    for (int j = 0 ; j< n;j++)
    if (matr[k][j] !=0)
    {
        long long tr = min(matr[i][k],matr[k][j]);
        matr[i][j]+= tr;
        matr[i][k]-=tr;
        matr[k][j]-=tr;

    }
    long long su = 0;
    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    su+= matr[i][j];
    cout << su;
}


#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 <cstring>
#include <cstdlib>
#include <ctime>

#define FOR(i,n) for (int i = 0 ; i< n;i++)
#define FORI(i,s,e) for (int i = s ; i<= e;i++)
#define MP(x,y) make_pair(x,y)
#define PB(x) push_back(x)
#define RALL(x) x.rbegin(),x.rend()
#define SZ(x) x.size()
#define FZ(x) memset(x,0,sizeof(x))

using namespace std;

long long matr[129][129];
int main()
{
    int n,m;
    cin >> n >> m;

    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    matr[i][j] = 0;

    for (int i = 0 ; i< m;i++)
    {
        int a , b,c;
        cin >> a >> b>>c;
        matr[a][b]=c;

    }
    for (int k = 0 ; k< n;k++)
    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    if ((matr[i][k] !=0)&&(matr[k][j] !=0))
    {
        long long tr = min(matr[i][k],matr[k][j]);
        matr[i][j]+= tr;
        matr[i][k]-=tr;
        matr[k][j]-=tr;

    }
    long long su = 0;
    for (int i = 0 ; i< n;i++)
    for (int j = 0 ; j< n;j++)
    su+= matr[i][j];
    cout << su;
}


#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 <cstring>
#include <cstdlib>
#include <ctime>

#define FOR(i,n) for (int i = 0 ; i< n;i++)
#define FORI(i,s,e) for (int i = s ; i<= e;i++)
#define MP(x,y) make_pair(x,y)
#define PB(x) push_back(x)
#define RALL(x) x.rbegin(),x.rend()
#define SZ(x) x.size()
#define FZ(x) memset(x,0,sizeof(x))

using namespace std;

long long matr[129][129];
int main()
{
    int n,m;
    cin >> n >> m;

    for (int i = 0 ; i< n+1;i++)
    for (int j = 0 ; j< n+1;j++)
    matr[i][j] = 0;

    for (int i = 0 ; i< m;i++)
    {
        int a , b,c;
        cin >> a >> b>>c;
        matr[a][b]=c;

    }
    //for (int te = 0 ; te< n;te++)
    for (int k = 0 ; k< n+1;k++)
    for (int i = 0 ; i< n+1;i++)
    if ((matr[i][k] !=0))
    for (int j = 0 ; j< n+1;j++)
    if (matr[k][j] !=0)
    {
        long long tr = min(matr[i][k],matr[k][j]);
        matr[i][j]+= tr;
        matr[i][k]-=tr;
        matr[k][j]-=tr;

    }
    long long su = 0;
    for (int i = 0 ; i< n+1;i++)
    for (int j = 0 ; j< n+1;j++)
    su+= matr[i][j];
    cout << su;
}


#include <bits/stdc++.h>
using namespace std ;
long long int n , m , a[200] , b[200] , ans ;

void input(){
    cin >> n >> m ;
    for ( int i = 0 ; i < m ; ++i ){
        int v1 , v2 , x ;
        cin >> v1 >> v2 >> x ;
//        v[v1].push_back(MP(v2,x)) ;
        a[v1] += x ;
        b[v2] += x ;
    }
}

main(){
    input() ;
    for ( int i = 1 ; i <= n ; ++i )
        ans += a[i] ;
    for ( int i = 1 ; i <= n ; ++i ){
        if ( b[i] == a[i] )
            ans -= a[i] ;
        else if ( b[i] > a[i] )
            ans -= a[i] ;
        else
            ans -= b[i] ;
    }
    cout << ans ;
}


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

void input(){
    cin >> n >> m ;
    for ( int i = 0 ; i < m ; ++i ){
        int v1 , v2 , x ;
        cin >> v1 >> v2 >> x ;
//        v[v1].push_back(MP(v2,x)) ;
        a[v1] += x ;
        b[v2] += x ;
    }
}

main(){
    input() ;
    for ( int i = 1 ; i <= n ; ++i )
        ans += a[i] ;
    for ( int i = 1 ; i <= n ; ++i ){
        if ( b[i] == a[i] )
            ans -= a[i] ;
        else if ( b[i] > a[i] )
            ans -= a[i] ;
        else
            ans -= b[i] ;
    }
    cout << ans ;
}


#include <iostream>
using namespace std;

const int MAXN = 100 + 10;
int v[MAXN], k[MAXN];

int main()
{
	int n, m;
	cin>> n >> m;

	for(int i = 0; i < m; i ++)
	{
		int a, b, c;
		cin>> a >> b >> c;
		v[b] += c;
		k[a] += c;
	}

	int ans = 0;
	for(int i = 0; i < n; i ++)
		if(k[i] - v[i] > 0)
			ans += k[i] - v[i];

	cout<< ans << endl;

}


#include <iostream>
using namespace std;

const int MAXN = 100 + 10;
int v[MAXN], k[MAXN];

int main()
{
	int n, m;
	cin>> n >> m;

	for(int i = 0; i < m; i ++)
	{
		int a, b, c;
		cin>> a >> b >> c;
		v[b] += c;
		k[a] += c;
	}

	int ans = 0;
	for(int i = 1; i <= n; i ++)
		if(v[i] > k[i])
			ans += v[i] - k[i];

	cout<< ans << endl;

}


#include <iostream>
using namespace std;

const int MAXN = 100 + 10;
int v[MAXN], k[MAXN];

int main()
{
	int n, m;
	cin>> n >> m;

	for(int i = 0; i < m; i ++)
	{
		int a, b, c;
		cin>> a >> b >> c;
		v[b] += c;
		k[a] += c;
	}

	int ans = 0;
	for(int i = 0; i < n; i ++)
		if(v[i] - k[i] > 0)
			ans += v[i] - k[i];

	cout<< ans << endl;

}


#include<iostream>
#include<cmath>
using namespace std;
int a[1000];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        a[x]-=z;
        a[y]+=z;
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>0)
        sum+=a[i];
        else
        sum-=a[i];
    }
    cout<<sum/2<<endl;
    //system("pause");
}


#include<iostream>
#include<cmath>
using namespace std;
int a[1000];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        a[x]-=z;
        a[y]+=z;
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=abs(a[i]);
    }
    cout<<sum/2<<endl;
    //system("pause");
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<utility>
#include<list>
#include<map>
#include<set>

#define mod 1000000007
#define make_pair MP
#define MS(A,a) memset(A,a,sizeof(A))
#define BITS_1(n) __builtin_popcount(n)
#define BITS_1l(n) __builtin_popcount(n)
#define BITS_1ll(n) __builtin_popcount(n)
#define X first
#define Y second
#define PB push_back
#define FOUND(A,x) (A.find(x)!=A.end())
#define getcx getchar_unlocked
#define INF 999999999
#define rep(i,n) for(i=0;i<n;i++)
#define PLIMIT 1000001
#define MAX 1000001
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<long long> VLL;
typedef pair<int,int> II;
typedef vector<bool> VB;
typedef map<int,int> MII;

bool PRIME[PLIMIT];
inline void inp( int &n ) {
n=0; int ch = getcx(); int sign = 1;
while(ch < '0' || ch > '9') { if(ch == '-') sign=-1; ch = getcx(); }
while(ch >= '0' && ch <= '9') { n = (n << 3) + (n << 1) + ch - '0', ch = getcx(); }
n = n * sign;
}

LL power(LL a,LL b){ if(b==0) return 1;LL d=power(a,b/2)%mod;return (((d*d)%mod)*(((b&1)?a:1)%mod))%mod;}
LL poweri(LL a,LL b,LL modi){ if(b==0) return 1;LL d=poweri(a,b/2,modi)%modi;return (((d*d)%modi)*(((b&1)?a:1)%modi))%modi;}
int gcd(int a,int b){ if(b==0) return a; else return gcd(b,a%b);}
void seive(){ int sq=sqrt(PLIMIT); for(int i=3;i<=sq;i+=2) { if(!PRIME[i])for(int j=i*i;j<=PLIMIT;j+=2*i) PRIME[j]=true;} for(int i=4;i<=PLIMIT;i+=2) PRIME[i]=true;}

unsigned int nextPowerOf2(unsigned int n)
{
    n--;
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    n++;
    return n;
}

int t,i,n,m;
int arr[200],ans=0;
int main(){
	inp(n);inp(m);
	for(i=0;i<m;i++){
		int x,y,z;cin>>x>>y>>z;
		arr[x-1]+=z;
		arr[y-1]-=z;
	}
	for(i=0;i<m;i++) if(arr[i]>0) ans+=arr[i];
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;

int t,i,n,m;
int arr[200],ans=0;
int main(){
	cin>>n>>m;
	for(i=0;i<m;i++){
		int x,y,z;cin>>x>>y>>z;
		arr[x-1]+=z;
		arr[y-1]-=z;
	}
	for(i=0;i<n;i++) if(arr[i]>0) ans+=arr[i];
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;

int t,i,n,m;
int arr[200],ans=0;
int main(){
	cin>>n>>m;
	for(i=0;i<m;i++){
		int x,y,z;cin>>x>>y>>z;
		arr[x-1]+=z;
		arr[y-1]-=z;
	}
	for(i=0;i<m;i++) if(arr[i]>0) ans+=arr[i];
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct {
  int totalIn;
  int totalOut;
  int debt[101];
  int owed[101];
} person;

person people[101];

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    people[i].totalIn = people[i].totalOut = 0;
    memset(people[i].debt + 1, 0, n * sizeof(int));
  }
  int totalDebt = 0;

  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    people[a].totalOut += c;
    people[b].totalIn += c;
    people[a].debt[b] = c;
    people[b].owed[a] = c;

    totalDebt += c;
  }

  while (true) {
    int ind = -1;
    for (int i = 1; i <= n; i++) {
      if (people[i].totalIn > 0 && people[i].totalOut > 0) {
	ind = i;
	break;
      }
    }
    if (ind == -1)
      break;

    while (people[ind].totalIn > 0 && people[ind].totalOut > 0) {
      int inInd = -1, outInd = -1;
      for (int i = 0; i < n; i++) {
	if (people[ind].owed[i] > 0)
	  inInd = i;
	if (people[ind].debt[i] > 0)
	  outInd = i;
      }
      int owed = people[ind].owed[inInd];
      int owes = people[ind].debt[outInd];
      int minVal = owed <= owes ? owed : owes;

      people[ind].owed[inInd] -= minVal;
      people[ind].debt[outInd] -= minVal;
      people[ind].totalIn -= minVal;
      people[ind].totalOut -= minVal;

      people[inInd].debt[ind] -= minVal;
      people[inInd].debt[outInd] += minVal;
      people[outInd].owed[ind] -= minVal;
      people[outInd].owed[inInd] += minVal;

      totalDebt -= minVal;

      /*
      cout << endl;
      for (int i = 1; i <= n; i++) {
	cout << i << ": \n";
	cout << "\t" << people[i].totalIn << endl;
	cout << "\t" << people[i].totalOut << endl;
      }
      cout << "continuing...\n";
      */
    }
  }

  cout << totalDebt << endl;

  /*
  cout << endl;
  for (int i = 1; i <= n; i++) {
    cout << i << ": \n";
    cout << "\t" << people[i].totalIn << endl;
    cout << "\t" << people[i].totalOut << endl;
  }
  */

  return 0;
}


#include <iostream>
#include <string.h>
using namespace std;

typedef struct {
  int totalIn;
  int totalOut;
  int debt[101];
  int owed[101];
} person;

person people[101];

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    people[i].totalIn = people[i].totalOut = 0;
    memset(people[i].debt + 1, 0, n * sizeof(int));
  }
  int totalDebt = 0;

  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    people[a].totalOut += c;
    people[b].totalIn += c;
    people[a].debt[b] = c;
    people[b].owed[a] = c;

    totalDebt += c;
  }

  while (true) {
    int ind = -1;
    for (int i = 1; i <= n; i++) {
      if (people[i].totalIn > 0 && people[i].totalOut > 0) {
	ind = i;
	break;
      }
    }
    if (ind == -1)
      break;

    while (people[ind].totalIn > 0 && people[ind].totalOut > 0) {
      int inInd = -1, outInd = -1;
      for (int i = 1; i <= n; i++) {
	if (people[ind].owed[i] > 0)
	  inInd = i;
	if (people[ind].debt[i] > 0)
	  outInd = i;
      }

      int owed = people[ind].owed[inInd];
      int owes = people[ind].debt[outInd];
      int minVal = owed <= owes ? owed : owes;

      people[ind].owed[inInd] -= minVal;
      people[ind].debt[outInd] -= minVal;
      people[ind].totalIn -= minVal;
      people[ind].totalOut -= minVal;

      people[inInd].debt[ind] -= minVal;
      people[inInd].debt[outInd] += minVal;
      people[outInd].owed[ind] -= minVal;
      people[outInd].owed[inInd] += minVal;

      totalDebt -= minVal;

      /*
      cout << endl;
      for (int i = 1; i <= n; i++) {
	cout << i << ": \n";
	cout << "\t" << people[i].totalIn << endl;
	cout << "\t" << people[i].totalOut << endl;
      }
      cout << "continuing...\n";
      */
    }
  }

  cout << totalDebt << endl;

  /*
  cout << endl;
  for (int i = 1; i <= n; i++) {
    cout << i << ": \n";
    cout << "\t" << people[i].totalIn << endl;
    cout << "\t" << people[i].totalOut << endl;
  }
  */

  return 0;
}


#include <iostream>
#include <string.h>
using namespace std;

typedef struct {
  int totalIn;
  int totalOut;
  int debt[101];
  int owed[101];
} person;

person people[101];

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    people[i].totalIn = people[i].totalOut = 0;
    memset(people[i].debt + 1, 0, n * sizeof(int));
  }
  int totalDebt = 0;

  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    people[a].totalOut += c;
    people[b].totalIn += c;
    people[a].debt[b] = c;
    people[b].owed[a] = c;

    totalDebt += c;
  }

  while (true) {
    int ind = -1;
    for (int i = 1; i <= n; i++) {
      if (people[i].totalIn > 0 && people[i].totalOut > 0) {
	ind = i;
	break;
      }
    }
    if (ind == -1)
      break;

    while (people[ind].totalIn > 0 && people[ind].totalOut > 0) {
      int inInd = -1, outInd = -1;
      for (int i = 0; i < n; i++) {
	if (people[ind].owed[i] > 0)
	  inInd = i;
	if (people[ind].debt[i] > 0)
	  outInd = i;
      }
      int owed = people[ind].owed[inInd];
      int owes = people[ind].debt[outInd];
      int minVal = owed <= owes ? owed : owes;

      people[ind].owed[inInd] -= minVal;
      people[ind].debt[outInd] -= minVal;
      people[ind].totalIn -= minVal;
      people[ind].totalOut -= minVal;

      people[inInd].debt[ind] -= minVal;
      people[inInd].debt[outInd] += minVal;
      people[outInd].owed[ind] -= minVal;
      people[outInd].owed[inInd] += minVal;

      totalDebt -= minVal;

      /*
      cout << endl;
      for (int i = 1; i <= n; i++) {
	cout << i << ": \n";
	cout << "\t" << people[i].totalIn << endl;
	cout << "\t" << people[i].totalOut << endl;
      }
      cout << "continuing...\n";
      */
    }
  }

  cout << totalDebt << endl;

  /*
  cout << endl;
  for (int i = 1; i <= n; i++) {
    cout << i << ": \n";
    cout << "\t" << people[i].totalIn << endl;
    cout << "\t" << people[i].totalOut << endl;
  }
  */

  return 0;
}


#include <iostream>
using namespace std;

typedef struct {
  int totalIn;
  int totalOut;
  int debt[101];
  int owed[101];
} person;

person people[101];

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    people[i].totalIn = people[i].totalOut = 0;
    memset(people[i].debt + 1, 0, n * sizeof(int));
  }
  int totalDebt = 0;

  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    people[a].totalOut += c;
    people[b].totalIn += c;
    people[a].debt[b] = c;
    people[b].owed[a] = c;

    totalDebt += c;
  }

  while (true) {
    int ind = -1;
    for (int i = 1; i <= n; i++) {
      if (people[i].totalIn > 0 && people[i].totalOut > 0) {
	ind = i;
	break;
      }
    }
    if (ind == -1)
      break;

    while (people[ind].totalIn > 0 && people[ind].totalOut > 0) {
      int inInd = -1, outInd = -1;
      for (int i = 0; i < n; i++) {
	if (people[ind].owed[i] > 0)
	  inInd = i;
	if (people[ind].debt[i] > 0)
	  outInd = i;
      }
      int owed = people[ind].owed[inInd];
      int owes = people[ind].debt[outInd];
      int min = owed <= owes ? owed : owes;

      people[ind].owed[inInd] -= min;
      people[ind].debt[outInd] -= min;
      people[ind].totalIn -= min;
      people[ind].totalOut -= min;

      people[inInd].debt[ind] -= min;
      people[inInd].debt[outInd] += min;
      people[outInd].owed[ind] -= min;
      people[outInd].owed[inInd] += min;

      totalDebt -= min;

      /*
      cout << endl;
      for (int i = 1; i <= n; i++) {
	cout << i << ": \n";
	cout << "\t" << people[i].totalIn << endl;
	cout << "\t" << people[i].totalOut << endl;
      }
      cout << "continuing...\n";
      */
    }
  }

  cout << totalDebt << endl;

  /*
  cout << endl;
  for (int i = 1; i <= n; i++) {
    cout << i << ": \n";
    cout << "\t" << people[i].totalIn << endl;
    cout << "\t" << people[i].totalOut << endl;
  }
  */

  return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,i,j,cnt=0;
    cin>>n>>m;
    int a[m][3];
    int c[100]={0};
    for(i=0;i<m;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>a[i][j];
        }
        c[a[i][0]]-=a[i][2];
        c[a[i][1]]+=a[i][2];
    }
    for(i=0;i<=n;i++)
        if(c[i]>0)
            cnt+=c[i];
    cout<<cnt;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,i,j,cnt=0;
    cin>>n>>m;
    int a[m][3];
    int c[100]={0};
    for(i=0;i<m;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>a[i][j];
        }
        c[a[i][0]]+=a[i][2];
        c[a[i][1]]-=a[i][2];
    }
    for(i=0;i<=n;i++)
        if(c[i]>0)
            cnt+=c[i];
    cout<<cnt;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,i,j,cnt=0;
    cin>>n>>m;
    int a[m][3];
    int c[105]={0};
    for(i=0;i<m;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>a[i][j];
        }
        c[a[i][0]]-=a[i][2];
        c[a[i][1]]+=a[i][2];
    }
    for(i=0;i<=n;i++)
        if(c[i]>0)
            cnt+=c[i];
    cout<<cnt;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,i,j,cnt=0;
    cin>>n>>m;
    int a[m][3];
    int c[100]={0};
    for(i=0;i<m;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>a[i][j];
        }
        c[a[i][0]]+=a[i][2];
        c[a[i][1]]-=a[i][2];
    }
    for(i=0;i<n;i++)
        if(c[i]>0)
            cnt+=c[i];
    cout<<cnt;
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100010
int a[maxn],b[maxn],c[maxn];
int main(void){

    int n,m;
    scanf("%d%d",&n,&m);
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     int sum = 0;
     rep(i,0,m){
      if(a[i] == b[i]) continue;
      rep(j,0,m){
       if(a[j] == b[j]) continue;
       if(b[i] == a[j])
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j]) c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[j]) c[i] = 0;
        }
       else if(a[i] == b[j]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         b[j] = b[i];
         if(a[j] == b[j]) c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[i]) c[i] = 0;
        }
       }
      }
     }
     rep(i,0,m)
      sum += c[i];
     printf("%d\n",sum);
    return 0;
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 10010

int a[maxn],b[maxn],c[maxn];

int main(void){

    freopen("in.txt","r",stdin);

    int n,m,sum;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     sum = 0;
     rep(i,0,m){
      if(a[i] == b[i])
       break;
      rep(j,i+1,m){
       if(a[j] == b[i]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[i])
          c[i] = 0;
        }
       }
      }
     }
     rep(i,0,m)
      sum+=c[i];
     printf("%d\n",sum);
    }
    return 0;
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100010
int a[maxn],b[maxn],c[maxn];
int main(void){

    int n,m;
    scanf("%d%d",&n,&m);
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     int sum = 0;
     rep(i,0,m){
      if(a[i] == b[i]) continue;
      rep(j,0,m){
       if(a[j] == b[j]) continue;
       if(b[i] == a[j])
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j]) c[j] = 0;
        }
        else{
         c[j] -= c[i];
         b[i] = b[j];
         if(a[i] == b[j]) c[i] = 0;
        }
       else if(a[i] == b[j]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         b[j] = b[i];
         if(a[j] == b[j]) c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[i]) c[i] = 0;
        }
       }
      }
     }
     rep(i,0,m)
      sum += c[i];
     printf("%d\n",sum);
    return 0;
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100010
int a[maxn],b[maxn],c[maxn];
int main(void){

    freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     int sum = 0;
     rep(i,0,m){
      if(a[i] == b[i]) break;
      rep(j,0,m){
       if(a[j] == b[j]) break;
       if(b[i] == a[j])
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j]) c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[j]) c[i] = 0;
        }
       else if(a[i] == b[j]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         b[j] = b[i];
         if(a[j] == b[j]) c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[i]) c[i] = 0;
        }
       }
      }
     }
     rep(i,0,m)
      sum += c[i];
     printf("%d\n",sum);
    }
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 10010

int a[maxn],b[maxn],c[maxn];

int main(void){

    int n,m,sum;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     sum = 0;
     rep(i,0,m-1)
      rep(j,i+1,m)
      if(a[j] == b[i]){
       if(a[i] == b[j]){
        c[i] = 0;
        c[j] = 0;
       }
       c[i] -= c[j];
       a[j] = a[i];
      }
     rep(i,0,m)
      sum+=c[i];
     printf("%d\n",sum);
    }
    return 0;
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 10010

int a[maxn],b[maxn],c[maxn];

int main(void){

    int n,m,sum;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     sum = 0;
     rep(i,0,m){
      if(a[i] == b[i])
       break;
      rep(j,i+1,m){
       if(a[j] == b[i]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
         if(a[i] == b[i])
          c[i] = 0;
        }
       }
      }
     }
     rep(i,0,m)
      sum+=c[i];
     printf("%d\n",sum);
    }
    return 0;
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100010
int a[maxn],b[maxn],c[maxn];
int main(void){

    freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     int sum = 0;
     rep(i,0,m){
      if(a[i] == b[i]) break;
      rep(j,0,m){
       if(b[i] == a[j])
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
        }
       else if(a[i] == b[j]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
        }
       }
      }
     }
     rep(i,0,m)
      sum += c[i];
     printf("%d\n",sum);
    }
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100010
int a[maxn],b[maxn],c[maxn];
int main(void){

    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     int sum = 0;
     rep(i,0,m){
      if(a[i] == b[i]) break;
      rep(j,0,m){
       if(b[i] == a[j])
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
        }
       else if(a[i] == b[j]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
        }
       }
      }
     }
     rep(i,0,m)
      sum += c[i];
     printf("%d\n",sum);
    }
}


#include <stdio.h>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100010
int a[maxn],b[maxn],c[maxn];
int main(void){

    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
     rep(i,0,m)
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
     int sum = 0;
     rep(i,0,m){
      if(c[i] == 0) break;
      rep(j,0,m){
       if(c[j] == 0) break;
       if(b[i] == a[j])
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
        }
       else if(a[i] == b[j]){
        if(c[i] >= c[j]){
         c[i] -= c[j];
         a[j] = a[i];
         if(a[j] == b[j])
          c[j] = 0;
        }
        else{
         c[j] -= c[i];
         a[i] = a[j];
        }
       }
      }
     }
     rep(i,0,m)
      sum += c[i];
     printf("%d\n",sum);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;

int a[N], b[N], vis[N];
int main() {
    int _;
    cin >> _;
    while (_--) {
        int n, m;
        scanf("%d%d", &n, &m);
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            if (i > 1 && a[i] < a[i - 1]) ok = 0;
        }
        if (ok) {
            puts("0");
            continue;
        }
        int ans = 0;
        while (1) {
            ok = 0;
            for (int i = 1; i <= n; i++) {
                if (a[i] > m) {
                    ok = 1;
                    swap(a[i], m);
                    ans++;
                }
            }
            if (ok == 0) break;
        }
        ok = 1;
        for (int i = 2; i <= n; i++) {
            if (a[i] < a[i - 1]) ok = 0;
        }
        if (ok)
            printf("%d\n", ans);
        else
            puts("-1");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;

int a[N], b[N], vis[N];
int main() {
    int _;
    cin >> _;
    while (_--) {
        int n, m;
        scanf("%d%d", &n, &m);
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            if (i > 1 && a[i] < a[i - 1]) ok = 0;
        }
        if (ok) {
            puts("0");
            continue;
        }
        int ans = 0;
        while (1) {
            ok = 0;
            for (int i = 1; i <= n; i++) {
                if (a[i] > m) {
                    ok = 1;
                    swap(a[i], m);
                    ans++;
                    break;
                }
            }
            if (ok == 0) {
                puts("-1");
                break;
            }
            ok = 1;
            for (int i = 2; i <= n; i++) {
                if (a[i] < a[i - 1]) ok = 0;
            }
            if (ok) {
                printf("%d\n", ans);
                break;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;

int a[N], b[N];
int main() {
    int _;
    cin >> _;
    while (_--) {
        int n, m;
        scanf("%d%d", &n, &m);
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            if (i > 1 && a[i] < a[i - 1]) ok = 0;
        }
        if (ok) {
            puts("0");
            continue;
        }
        int ans = n + 1;

        for (int i = 1; i <= n; i++) {
            if (a[i] <= m) continue;
            for (int j = 1; j <= n; j++) {
                b[j] = j == i ? m : a[j];
            }
            sort(b + 1, b + n + 1);
            int cnt = 0;
            ok = 1;
            for (int j = 1; j <= n; j++) {
                if (a[j] != b[j]) {
                    //printf("%d %d %d %d\n", i, j, a[j], m);
                    if (a[j] <= m) {
                        ok = 0;
                        break;
                    }
                    cnt++;
                }
            }
            if (ok)
                ans = min(ans, cnt);
        }
        if (ans == n + 1) ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;

int a[N], b[N], vis[N];
int main() {
    int _;
    cin >> _;
    while (_--) {
        int n, m;
        scanf("%d%d", &n, &m);
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            if (i > 1 && a[i] < a[i - 1]) ok = 0;
        }
        if (ok) {
            puts("0");
            continue;
        }
        int ans = n + 1;

        for (int i = 1; i <= n; i++) {
            if (a[i] <= m) continue;
            for (int j = 1; j <= n; j++) {
                b[j] = j == i ? m : a[j];
            }
            sort(b + 1, b + n + 1);
            int cnt = 0;
            ok = 1;
            memset(vis, 0, sizeof vis);
            for (int j = 1; j <= n; j++) {
                if (a[j] != b[j]) {
                    //printf("%d %d %d %d\n", i, j, a[j], m);
                    if (vis[a[j]] || a[j] <= m) {
                        ok = 0;
                        break;
                    }
                    vis[a[j]] = 1;
                    cnt++;
                }
            }
            if (ok)
                ans = min(ans, cnt);
        }
        if (ans == n + 1) ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;

int a[N], b[N], vis[N];
int main() {
    int _;
    cin >> _;
    while (_--) {
        int n, m;
        scanf("%d%d", &n, &m);
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            if (i > 1 && a[i] < a[i - 1]) ok = 0;
        }
        if (ok) {
            puts("0");
            continue;
        }
        int ans = 0;
        while (1) {
            ok = 0;
            for (int i = 1; i <= n; i++) {
                if (a[i] > m) {
                    ok = 1;
                    swap(a[i], m);
                    ans++;
                    break;
                }
            }
            if (ok == 0) break;
        }
        ok = 1;
        for (int i = 2; i <= n; i++) {
            if (a[i] < a[i - 1]) ok = 0;
        }
        if (ok)
            printf("%d\n", ans);
        else
            puts("-1");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[505];
int vis[505];
struct node
{
	int pos,val;
	node(int pos,int val):pos(pos),val(val){
	}
	friend bool operator<(node x,node y)
	{
		return x.val>y.val;
	}
};
priority_queue<node> q;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		while(!q.empty()) q.pop();
		memset(vis,0,sizeof(vis));
		int n,x;
		scanf("%d%d",&n,&x);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]) q.push(node(i,a[i])),vis[i]=1;
		}
		int ans=0;
		while(!q.empty())
		{
			int pos=q.top().pos;
			//printf("pos %d\n",pos);
			int cnt=0,mem=0;
			for(int j=pos;j>=1;j--)
			{
				if(a[j]<=x)
				{
					mem=j;
					break;
				}
				cnt++;
			}
			for(int j=mem+1;j<=pos;j++)
			{
				int tmp=a[j];
				a[j]=x;
				x=tmp;
			}
			if(cnt==0) break;
			ans+=cnt;
			q.pop();
			vis[pos]=0;
			for(int i=1;i<=n-1;i++)
			{
				if(a[i]>a[i+1]&&vis[i]==0) q.push(node(i,a[i])),vis[i]=1;
			}
		}
		//for(int i=1;i<=n;i++) printf("a %d\n",a[i]);
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1])
			{
				ans=-1;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
1
4 3
2 3 5 4
*/


#include<bits/stdc++.h>
using namespace std;
int a[505];
int aa[505];
int vis[505];
struct node
{
	int pos,val;
	node(int pos,int val):pos(pos),val(val){
	}
	friend bool operator<(node x,node y)
	{
		return x.val>y.val;
	}
};
priority_queue<node> q;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		while(!q.empty()) q.pop();
		memset(vis,0,sizeof(vis));
		int n,x;
		scanf("%d%d",&n,&x);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),aa[i]=a[i];
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]) q.push(node(i,a[i])),vis[i]=1;
		}
		int ans=0;
		while(!q.empty())
		{
			int pos=q.top().pos;
			//printf("pos %d\n",pos);
			int cnt=0,mem=0;
			for(int j=pos;j>=1;j--)
			{
				if(a[j]<=x)
				{
					mem=j;
					break;
				}
				cnt++;
			}
			for(int j=mem+1;j<=pos;j++)
			{
				int tmp=a[j];
				a[j]=x;
				x=tmp;
			}
			if(cnt==0) break;
			ans+=cnt;
			q.pop();
			vis[pos]=0;
			for(int i=1;i<=n-1;i++)
			{
				if(a[i]>a[i+1]&&vis[i]==0) q.push(node(i,a[i])),vis[i]=1;
			}
		}
		//for(int i=1;i<=n;i++) printf("a %d\n",a[i]);
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1])
			{
				ans=-1;
				printf("%d\n",ans);
				break;
			}
		}
		int num=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!=aa[i]) num++;
		}
		printf("%d\n",min(ans,num));
	}
	return 0;
}
/*
1
4 3
2 3 5 4
*/


#include<bits/stdc++.h>
using namespace std;
int a[505];
struct node
{
	int pos,val;
	node(int pos,int val):pos(pos),val(val){
	}
};
vector<node> v;
int cmp1(node x,node y)
{
	return x.val<y.val;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		v.clear();
		int n,x;
		scanf("%d%d",&n,&x);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]) v.push_back(node(i,a[i]));
		}
		sort(v.begin(),v.end(),cmp1);
		int ans=0;
		for(int i=0;i<v.size();i++)
		{
			int pos=v[i].pos;
			int cnt=0,mem=0;
			for(int j=pos;j>=1;j--)
			{
				if(a[j]<=x)
				{
					mem=j;
					break;
				}
				cnt++;
			}
			for(int j=mem+1;j<=pos;j++)
			{
				int tmp=a[j];
				a[j]=x;
				x=tmp;
			}
			ans+=cnt;
			//printf("cnt%d\n",cnt);
		}
		//for(int i=1;i<=n;i++) printf("a %d\n",a[i]);
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1])
			{
				ans=-1;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
1
4 3
2 3 5 4
*/


#include<bits/stdc++.h>
using namespace std;
int a[505];
int aa[505];
int vis[505];
struct node
{
	int pos,val;
	node(int pos,int val):pos(pos),val(val){
	}
	friend bool operator<(node x,node y)
	{
		return x.val>y.val;
	}
};
priority_queue<node> q;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		while(!q.empty()) q.pop();
		memset(vis,0,sizeof(vis));
		int n,x;
		scanf("%d%d",&n,&x);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),aa[i]=a[i];
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]) q.push(node(i,a[i])),vis[i]=1;
		}
		int ans=0;
		while(!q.empty())
		{
			int pos=q.top().pos;
			//printf("pos %d\n",pos);
			int cnt=0,mem=0;
			for(int j=pos;j>=1;j--)
			{
				if(a[j]<=x)
				{
					mem=j;
					break;
				}
				cnt++;
			}
			for(int j=mem+1;j<=pos;j++)
			{
				int tmp=a[j];
				a[j]=x;
				x=tmp;
			}
			if(cnt==0) break;
			ans+=cnt;
			q.pop();
			vis[pos]=0;
			for(int i=1;i<=n-1;i++)
			{
				if(a[i]>a[i+1]&&vis[i]==0) q.push(node(i,a[i])),vis[i]=1;
			}
		}
		//for(int i=1;i<=n;i++) printf("a %d\n",a[i]);
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1])
			{
				ans=-1;

			}
		}
		int num=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!=aa[i]) num++;
		}
		if(ans==-1) printf("-1\n");
		else printf("%d\n",min(ans,num));
	}
	return 0;
}
/*
1
4 3
2 3 5 4
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	int res = 0, f = 0; char c = getchar();
	while(!isdigit(c) && c != '-') c = getchar();
	if(c == '-') f++, c = getchar();
	while(isdigit(c)) res = (res<<3)+(res<<1)+(c^48), c = getchar();
	return f ? -res : res;
}

const int maxn = 5e2+10;
int n, x, a[maxn], b[maxn], c[maxn], aa[maxn], cnt;
int vis[maxn];

int main()
{
	int T = read(); while(T--)
	{
		n = read(); x = read();
		int ff = 1;
		for(int i = 1; i <= n; i++) a[i] = read(), ff &= a[i] >= a[i-1];
		if(ff)
		{
			printf("0\n");
			continue;
		}
		int ans = 1e9;
		for(int i = 1; i <= n; i++) if(a[i] > x)
		{
			for(int j = 1; j <= n; j++) aa[j] = a[j];
			aa[i] = x;
			int sum = 0;
			for(int j = 1; j < i; j++) if(aa[j] > aa[i])
			{
				swap(aa[i], aa[j]);
				sum++;
			}
			int flag = 1;
			for(int i = 1; i < n; i++) flag &= aa[i] <= aa[i+1];
			if(flag) ans = min(ans, sum+1);
		}
		printf("%d\n", ans == 1e9 ? -1 : ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	int res = 0, f = 0; char c = getchar();
	while(!isdigit(c) && c != '-') c = getchar();
	if(c == '-') f++, c = getchar();
	while(isdigit(c)) res = (res<<3)+(res<<1)+(c^48), c = getchar();
	return f ? -res : res;
}

const int maxn = 5e2+10;
int n, x, a[maxn], aa[maxn];

int main()
{
	int T = read(); while(T--)
	{
		n = read(); x = read();
		for(int i = 1; i <= n; i++) a[i] = read();
		int pos = 0;
		for(int i = n-1; i; i--) if(a[i] > a[i+1])
		{
			pos = i; break;
		}
		int ans = 0, flag = 1;
		for(int i = 1; i <= pos && flag; i++)
		{
			if(a[i] > x)
				swap(x, a[i]), ans++;
			if(a[i] > a[i+1]) flag = 0;
		}
		printf("%d\n", flag ? ans : -1);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	int res = 0, f = 0; char c = getchar();
	while(!isdigit(c) && c != '-') c = getchar();
	if(c == '-') f++, c = getchar();
	while(isdigit(c)) res = (res<<3)+(res<<1)+(c^48), c = getchar();
	return f ? -res : res;
}

const int maxn = 5e2+10;
int n, x, a[maxn], b[maxn], c[maxn], aa[maxn], cnt;
int vis[maxn];

int main()
{
	int T = read(); while(T--)
	{
		n = read(); x = read();
		int ff = 1; a[0] = -1;
		for(int i = 1; i <= n; i++) a[i] = read(), ff &= a[i] >= a[i-1];
		if(ff)
		{
			printf("0\n");
			continue;
		}
		int ans = 1e9;
		for(int top = x; top <= 500; top++)
		{
			cnt = 0;
			memset(vis, 0, (n+5)<<2);
			for(int i = 1; i <= n; i++) if(a[i] > top)
			{
				b[++cnt] = c[cnt] = a[i];
				vis[i] = 1;
			}
			if(cnt == 0) break;
			sort(c+1, c+cnt+1);
			for(int i = cnt; i-1; i--) c[i] = c[i-1];
			c[1] = x;
			int flag = 1, pre = -1, sum = 0;
			for(int i = 1; i <= cnt; i++) if(b[i] != c[i])
			{
				flag &= b[i] > pre;
				pre = b[i];
				sum++;
			}
			if(!flag) continue;
			for(int i = n; i; i--)
			{
				if(vis[i]) aa[i] = c[cnt--];
				else aa[i] = a[i];
				if(i < n) flag &= aa[i] <= aa[i+1];
			}
			if(flag) ans = min(ans, sum);
		}
		printf("%d\n", ans == 1e9 ? -1 : ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	int res = 0, f = 0; char c = getchar();
	while(!isdigit(c) && c != '-') c = getchar();
	if(c == '-') f++, c = getchar();
	while(isdigit(c)) res = (res<<3)+(res<<1)+(c^48), c = getchar();
	return f ? -res : res;
}

const int maxn = 5e2+10;
int n, x, a[maxn], b[maxn], c[maxn], aa[maxn], cnt;
int vis[maxn];

int main()
{
	int T = read(); while(T--)
	{
		n = read(); x = read();
		int ff = 1; a[0] = -1;
		for(int i = 1; i <= n; i++) a[i] = read(), ff &= a[i] >= a[i-1];
		if(ff)
		{
			printf("0\n");
			continue;
		}
		int ans = 1e9;
		for(int top = x; top <= 500; top++)
		{
			cnt = 0;
			memset(vis, 0, (n+5)<<2);
			for(int i = 1; i <= n; i++) if(a[i] > x && x <= top)
			{
				b[++cnt] = c[cnt] = a[i];
				vis[i] = 1;
			}
			sort(c+1, c+n+1);
			for(int i = cnt; i-1; i--) c[i] = c[i-1];
			c[1] = x;
			int flag = 1, pre = -1, sum = 0;
			for(int i = 1; i <= cnt; i++) if(b[i] != c[i])
			{
				flag &= b[i] > pre;
				pre = b[i];
				sum++;
			}
			if(!flag) continue;
			for(int i = n; i; i--)
			{
				if(vis[i]) aa[i] = c[cnt--];
				else aa[i] = a[i];
				if(i < n) flag &= aa[i] <= aa[i+1];
			}
			if(flag) ans = min(ans, sum);
		}
		printf("%d\n", ans == 1e9 ? -1 : ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	int res = 0, f = 0; char c = getchar();
	while(!isdigit(c) && c != '-') c = getchar();
	if(c == '-') f++, c = getchar();
	while(isdigit(c)) res = (res<<3)+(res<<1)+(c^48), c = getchar();
	return f ? -res : res;
}

const int maxn = 5e2+10;
int n, x, a[maxn], aa[maxn];

int main()
{
	int T = read(); while(T--)
	{
		n = read(); x = read();
		for(int i = 1; i <= n; i++) a[i] = read();
		a[n+1] = 501;
		int ans = 0, flag = 1;
		for(int i = 1; i <= n && flag; i++)
		{
			if(a[i] > x)
				swap(x, a[i]), ans++;
			if(a[i] > a[i+1]) flag = 0;
		}
		printf("%d\n", flag ? ans : -1);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	int res = 0, f = 0; char c = getchar();
	while(!isdigit(c) && c != '-') c = getchar();
	if(c == '-') f++, c = getchar();
	while(isdigit(c)) res = (res<<3)+(res<<1)+(c^48), c = getchar();
	return f ? -res : res;
}

const int maxn = 5e2+10;
int n, x, a[maxn], b[maxn], c[maxn], aa[maxn], cnt;
int vis[maxn];

int main()
{
	int T = read(); while(T--)
	{
		n = read(); x = read();
		int ff = 1; a[0] = -1;
		for(int i = 1; i <= n; i++) a[i] = read(), ff &= a[i] >= a[i-1];
		if(ff)
		{
			printf("0\n");
			continue;
		}
		int ans = 1e9;
		for(int top = x; top <= 500; top++)
		{
			cnt = 0;
			memset(vis, 0, (n+5)<<2);
			for(int i = 1; i <= n; i++) if(a[i] > x && x <= top)
			{
				b[++cnt] = c[cnt] = a[i];
				vis[i] = 1;
			}
			sort(c+1, c+cnt+1);
			for(int i = cnt; i-1; i--) c[i] = c[i-1];
			c[1] = x;
			int flag = 1, pre = -1, sum = 0;
			for(int i = 1; i <= cnt; i++) if(b[i] != c[i])
			{
				flag &= b[i] > pre;
				pre = b[i];
				sum++;
			}
			if(!flag) continue;
			for(int i = n; i; i--)
			{
				if(vis[i]) aa[i] = c[cnt--];
				else aa[i] = a[i];
				if(i < n) flag &= aa[i] <= aa[i+1];
			}
			if(flag) ans = min(ans, sum);
		}
		printf("%d\n", ans == 1e9 ? -1 : ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1]||x>=a[i])ok=false;
				int j=i-1;
				while(j>=1&&a[j]>x)j--;
				for(int z=j+1;z<=i;z++)swap(x,a[z]);
				ans+=i-j;//x=a[i];
				if(a[i]>a[i+1])ok=false;
			}
		}
	//	for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
		if(!ok){
			puts("-1");
		}
		else printf("%d\n",ans);

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1]||x>=a[i])ok=false;
				int j=i-1;
				while(j>=1&&a[j]>x)j--;
				for(int z=j+1;z<=i;z++){
					swap(x,a[z]);
				}
				ans+=i-j;//x=a[i];
				if(a[i]>a[i+1])ok=false;
			}
		}
		for(int i=1;i<n;i++)if(a[i]>a[i+1])ok=false;
		//for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
		if(!ok){
			puts("-1");
		}
		else printf("%d\n",ans);

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1]||x>=a[i])ok=false;
				int j=i-1;
				while(j>=1&&a[j]>x)j--;
				for(int z=j+1;z<=i;z++){
					swap(x,a[z]);
				}
				ans+=i-j;//x=a[i];
				if(a[i]>a[i+1])ok=false;
			}
		}
		for(int i=1;i<n;i++)if(a[i]>a[i+1])ok=false;
		for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
		if(!ok){
			puts("-1");
		}
		else printf("%d\n",ans);

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1])ok=false;
				int j=i;
				while(j-1>=1&&a[j-1]>x)j--;
				for(int z=j-1;z<=i;z++)swap(x,a[z]);
				ans+=i-j+1;//x=a[i];
			}
		}
		if(!ok){
			puts("-1");continue;
		}
		else printf("%d\n",ans);

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1010;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1]||x>=a[i])ok=false;
				int j=i-1;
				while(j>=1&&a[j]>x)j--;
				for(int z=j+1;z<=i;z++){
					swap(x,a[z]);
				}
				ans+=i-j;//x=a[i];
				if(a[i]>a[i+1])ok=false;
			}
		}
		for(int i=1;i<n;i++)if(a[i]>a[i+1])ok=false;
		//for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
		if(!ok){
			puts("-1");
		}
		else printf("%d\n",ans);

	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=550;
int a[N];
int main()
{
     int T;cin>>T;
	 while(T--)
	 {
	     int n,x;cin>>n>>x;
		 for(int i=1;i<=n;i++)cin>>a[i];
		 int ans=0;
		 while(!is_sorted(a+1,a+1+n))
		 {
		       int pos=0;
			   for(int i=1;i<=n;i++)if(a[i]>x){
			   	ans++;swap(x,a[i]);pos=i;break;
			   }
			   if(!pos){
			   	ans=-1;break;
			   }
		 }
		 cout<<ans<<endl;
	 }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1])ok=false;
				int j=i-1;
				while(j>=1&&a[j]>x)j--;
				for(int z=j+1;z<=i;z++)swap(x,a[z]);
				ans+=i-j;//x=a[i];
				if(a[i]>a[i+1])ok=false;
			}
		}
		//for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
		if(!ok){
			puts("-1");
		}
		else printf("%d\n",ans);

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
int a[N];
int main()
{
	int T;cin>>T;
	while(T--)
	{
		int n,x;cin>>n>>x;
		for(int i=1;i<=n;i++)cin>>a[i];
		bool ok=true;
		int ans=0;
		for(int i=1;i<n;i++){
			if(a[i]>a[i+1]){
				if(x>a[i+1])ok=false;
				int j=i;
				while(j-1>=1&&a[j-1]>x)j--;
				ans+=i-j+1;x=a[i];
			}
		}
		if(!ok){
			puts("-1");continue;
		}
		else printf("%d\n",ans);

	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define mod 1000000007
#define v_int vector<int>
#define v_ll vector<ll>
#define v_str vector<string>
#define vv_int vector<vector<int>>
#define v_pair vector<pair<int,int>>
#define loop(x,n) for(int i=x;i<n;++i)
#define input_v(arr,n) for(int i=0;i<n;++i) cin>>arr[i];

int main() {
    //is_prime();
    int t=1;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        v_int arr(n,0);
        //v_str arr(n,0);
        input_v(arr,n);
        int ans = 0;
        int tmp;
        loop(0,n){
            if(arr[i]>x){
                ++ans;
                tmp = x;
                x = arr[i];
                arr[i] = tmp;
            }
        }
        loop(1,n){
            if(arr[i]<arr[i-1]){
                ans = -1;
                break;
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define mod 1000000007
#define v_int vector<int>
#define v_ll vector<ll>
#define v_str vector<string>
#define vv_int vector<vector<int>>
#define v_pair vector<pair<int,int>>
#define loop(x,n) for(int i=x;i<n;++i)
#define input_v(arr,n) for(int i=0;i<n;++i) cin>>arr[i];

int main() {

    auto if_sort = [&](int n, v_int vec){
        loop(1,n){
            if(vec[i]<vec[i-1]){
                return false;
            }
        }
        return true;
    };

    int t=1;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        v_int arr(n,0);
        //v_str arr(n,0);
        input_v(arr,n);
        int ans = 0;
        int tmp=0;
        if(!if_sort(n,arr)){
            loop(0,n-1){
                if(arr[i]>x){
                    ++ans;
                    tmp = x;
                    x = arr[i];
                    arr[i] = tmp;
                    if(if_sort(n,arr)){
                        break;
                    }
                }
            }
        }
        if(!if_sort(n,arr))
            ans = -1;
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define mod 1000000007
#define v_int vector<int>
#define v_ll vector<ll>
#define v_str vector<string>
#define vv_int vector<vector<int>>
#define v_pair vector<pair<int,int>>
#define loop(x,n) for(int i=x;i<n;++i)
#define input_v(arr,n) for(int i=0;i<n;++i) cin>>arr[i];

int main() {
    //is_prime();
    int t=1;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        v_int arr(n,0);
        //v_str arr(n,0);
        input_v(arr,n);
        int ans = 0;
        int tmp;
        bool sorted = true;
        loop(1,n){
            if(arr[i]<arr[i-1]){
                sorted = false;
                break;
            }
        }
        if(!sorted){
            loop(0,n-1){
                if(arr[i]>x){
                    ++ans;
                    tmp = x;
                    x = arr[i];
                    arr[i] = tmp;
                }
            }
            loop(1,n){
                if(arr[i]<arr[i-1]){
                    ans = -1;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define mod 1000000007
#define v_int vector<int>
#define v_ll vector<ll>
#define v_str vector<string>
#define vv_int vector<vector<int>>
#define v_pair vector<pair<int,int>>
#define loop(x,n) for(int i=x;i<n;++i)
#define input_v(arr,n) for(int i=0;i<n;++i) cin>>arr[i];

int main() {
    //is_prime();
    int t=1;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        v_int arr(n,0);
        //v_str arr(n,0);
        input_v(arr,n);
        int ans = 0;
        int tmp=0;
        bool sorted = true;
        loop(1,n){
            if(arr[i]<arr[i-1]){
                sorted = false;
                break;
            }
        }
        if(!sorted||(n!=1)){
            loop(0,n-1){
                if(arr[i]>x){
                    ++ans;
                    tmp = x;
                    x = arr[i];
                    arr[i] = tmp;
                }
            }
            loop(1,n){
                if(arr[i]<arr[i-1]){
                    ans = -1;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define mod 1000000007
#define v_int vector<int>
#define v_ll vector<ll>
#define v_str vector<string>
#define vv_int vector<vector<int>>
#define v_pair vector<pair<int,int>>
#define loop(x,n) for(int i=x;i<n;++i)
#define input_v(arr,n) for(int i=0;i<n;++i) cin>>arr[i];

int main() {
    //is_prime();
    int t=1;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        v_int arr(n,0);
        //v_str arr(n,0);
        input_v(arr,n);
        int ans = 0;
        int tmp;
        bool sorted = true;
        loop(1,n){
            if(arr[i]<arr[i-1]){
                sorted = false;
                break;
            }
        }
        if(!sorted){
            loop(0,n){
                if(arr[i]>x){
                    ++ans;
                    tmp = x;
                    x = arr[i];
                    arr[i] = tmp;
                }
            }
            loop(1,n){
                if(arr[i]<arr[i-1]){
                    ans = -1;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include <cstdio>
#include <cctype>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define ll long long
#define ls id<<1
#define rs id<<1|1
template <class T>
void read(T &x)
{
    int f=0;x=0;char c=getchar();
    while(!isdigit(c)) f=c=='-'?1:0,c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    if(f) x=-x;
}
const int N=510;
int n,x,a[N];
void work()
{
	read(n),read(x);
	int ans=0;a[0]=x;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int las=1,i=2;i<=n;i++)
	{
		if(a[i]<a[i-1])
		{
			while(x>=a[las]&&las<i) ++las;
			if(a[i-2]>a[i]||x>=a[las])
			{
				puts("-1");
				return;
			}
			x=a[i-1];
			ans+=i-las;
			las=i+1;
		}
	}
	printf("%d\n",ans);
}
int main()
{
	int t;read(t);
	while(t--)
	{
		work();
	}
	return 0;
}


#include <cstdio>
#include <cctype>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define ll long long
#define ls id<<1
#define rs id<<1|1
template <class T>
void read(T &x)
{
    int f=0;x=0;char c=getchar();
    while(!isdigit(c)) f=c=='-'?1:0,c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    if(f) x=-x;
}
const int N=510;
int n,x,a[N];
void work()
{
	read(n),read(x);
	int ans=0;a[0]=x;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int las=1,i=2;i<=n;i++)
	{
		if(a[i]<a[i-1])
		{
			if(a[i-2]>a[i]||x>=a[las])
			{
				puts("-1");
				return;
			}
			x=a[i-1];
			ans+=i-las;
			las=i+1;
		}
	}
	printf("%d\n",ans);
}
int main()
{
	int t;read(t);
	while(t--)
	{
		work();
	}
	return 0;
}


#include <cstdio>
#include <cctype>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define ll long long
#define ls id<<1
#define rs id<<1|1
template <class T>
void read(T &x)
{
    int f=0;x=0;char c=getchar();
    while(!isdigit(c)) f=c=='-'?1:0,c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    if(f) x=-x;
}
const int N=510;
int n,x,a[N];
void work()
{
	read(n),read(x);
	int ans=0;a[0]=x;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=2;i<=n;i++)
	{
		if(a[i]<a[i-1])
		{
			int p=i;
			for(int j=1;j<i;j++)
				if(x<a[j])
				{
					p=j;
					break;
				}
			int t=x;
			x=a[i-1];
			for(int j=i-1;j>p;j--) a[j]=a[j-1];
			a[p]=t;
			if(a[i]<a[i-1]||p==i)
			{
				puts("-1");
				return;
			}
			ans+=i-p;
		}
	}
	printf("%d\n",ans);
}
int main()
{
	int t;read(t);
	while(t--)
	{
		work();
	}
	return 0;
}


#include <cstdio>
#include <cctype>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define ll long long
#define ls id<<1
#define rs id<<1|1
template <class T>
void read(T &x)
{
    int f=0;x=0;char c=getchar();
    while(!isdigit(c)) f=c=='-'?1:0,c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    if(f) x=-x;
}
const int N=510;
int n,x,a[N];
void work()
{
	read(n),read(x);
	int ans=0;a[0]=x;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=2;i<=n;i++)
	{
		if(a[i]<a[i-1])
		{
			int p=i;
			for(int j=1;j<i;j++)
				if(x<a[j])
				{
					p=j;
					break;
				}
			int t=x;
			x=a[i-1];
			for(int j=i-1;j>p;j--) ans+=a[j]!=a[j-1],a[j]=a[j-1];
			a[p]=t;
			if(a[i]<a[i-1]||p==i)
			{
				puts("-1");
				return;
			}
		}
	}
	printf("%d\n",ans);
}
int main()
{
	int t;read(t);
	while(t--)
	{
		work();
	}
	return 0;
}


#include <cstdio>
#include <cctype>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define ll long long
#define ls id<<1
#define rs id<<1|1
template <class T>
void read(T &x)
{
    int f=0;x=0;char c=getchar();
    while(!isdigit(c)) f=c=='-'?1:0,c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    if(f) x=-x;
}
const int N=510;
int n,x,a[N];
void work()
{
	read(n),read(x);
	int ans=0;a[0]=x;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=2;i<=n;i++)
	{
		if(a[i]<a[i-1])
		{
			int p=i;
			for(int j=1;j<i;j++)
				if(x<a[j])
				{
					p=j;
					break;
				}
			int t=x;
			x=a[i-1];
			for(int j=i-1;j>p;j--) ans+=a[j]!=a[j-1],a[j]=a[j-1];
			++ans;
			a[p]=t;
			if(a[i]<a[i-1]||p==i)
			{
				puts("-1");
				return;
			}
		}
	}
	printf("%d\n",ans);
}
int main()
{
	int t;read(t);
	while(t--)
	{
		work();
	}
	return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int t;
int n,x,a[N];
int check(int l,int r){
  int st=1;
  for(int i=l+1;i<=r;i++){
    st&=(a[i]>=a[i-1]);
  }
  return st;
}
int get_min(int l,int r){
  int res=1e9;
  for(int i=l;i<=r;i++){
    res=min(res,a[i]);
  }
  return res;
}
int solve(){
  int now=0;
  a[0]=-1;
  while(1){
    if(check(1,n))return now;
    int res=0;
    for(int i=1;i<=n;i++){
      if(check(1,i-1)&&a[i-1]<=x && a[i]>x && x<=get_min(i+1,n)){
        res=1;
        swap(a[i],x);
        ++now;
        break;
      }
    }
    if(!res)return -1;
  }
}
int main() {
  ios::sync_with_stdio(false);
  for(cin>>t;t;t--){
    cin>>n>>x;
    for(int i=1;i<=n;i++)cin>>a[i];
    cout<<solve()<<'\n';
  }
  return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int t;
int n,x,a[N];
int check(int l,int r){
  int st=1;
  for(int i=l+1;i<=r;i++){
    st&=(a[i]>=a[i-1]);
  }
  return st;
}
int get_min(int l,int r){
  int res=1e9;
  for(int i=l;i<=r;i++){
    res=min(res,a[i]);
  }
  return res;
}
int solve(){
  int now=0;
  a[0]=-1;
  while(1){
    if(check(1,n))return now;
    int res=0;
    for(int i=1;i<=n;i++){
      if(check(1,i-1)&&a[i-1]<x && a[i]>x && x<=get_min(i+1,n)){
        res=1;
        swap(a[i],x);
        ++now;
        break;
      }
    }
    if(!res)return -1;
  }
}
int main() {
  ios::sync_with_stdio(false);
  for(cin>>t;t;t--){
    cin>>n>>x;
    for(int i=1;i<=n;i++)cin>>a[i];
    cout<<solve()<<'\n';
  }
  return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int t;
int n,x,a[N];
int check(int l,int r){
  int st=1;
  for(int i=l+1;i<=r;i++){
    st&=(a[i]>=a[i-1]);
  }
  return st;
}
int solve(){
  int now=0;
  while(1){
    if(check(1,n))return now;
    int res=0;
    for(int i=n;i>=1;i--){
      if(check(1,i-1)&&a[i-1]<x && a[i]>x){
        res=1;
        swap(a[i],x);
        ++now;
        break;
      }
    }
    if(!res)return -1;
  }
}
int main() {
  ios::sync_with_stdio(false);
  for(cin>>t;t;t--){
    cin>>n>>x;
    for(int i=1;i<=n;i++)cin>>a[i];
    cout<<solve()<<'\n';
  }
  return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int t;
int n,x,a[N];
int check(int l,int r){
  int st=1;
  for(int i=l+1;i<=r;i++){
    st&=(a[i]>=a[i-1]);
  }
  return st;
}
int solve(){
  int now=0;
  a[0]=-1;
  while(1){
    if(check(1,n))return now;
    int res=0;
    for(int i=n;i>=1;i--){
      if(check(1,i-1)&&a[i-1]<x && a[i]>x){
        res=1;
        swap(a[i],x);
        ++now;
        break;
      }
    }
    if(!res)return -1;
  }
}
int main() {
  ios::sync_with_stdio(false);
  for(cin>>t;t;t--){
    cin>>n>>x;
    for(int i=1;i<=n;i++)cin>>a[i];
    cout<<solve()<<'\n';
  }
  return 0;
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 510;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

int a[N], b[N], n, x;

int main()
{
#ifdef OlaMins
	freopen("D:/input.txt", "r", stdin);
	//freopen("D:/output.txt", "w", stdout);
#endif

	int T; cin >> T;
	while (T--) {
		sc("%d %d", &n, &x);
		for (int i = 1; i <= n; i++)
			sc("%d", &a[i]);
		if (is_sorted(a + 1, a + n + 1)) {
			puts("0");
			continue;
		}

		int ans = INF;
		for (int s = 1; s <= n; s++) { // 枚举x在哪个位置
			MPY(b, a);
			int t = x, tot = 1;
			swap(b[s], t);

			for (int i = s + 1; i < n; i++) {
				if (b[i] > t)
					swap(b[i], t), tot++;
			}

			if (is_sorted(b + 1, b + n + 1))
				Min(ans, tot);
		}
		if (ans == INF)
			puts("-1");
		else
			printf("%d\n", ans);
	}
	return 0; // 改数组大小!!!用pair改宏定义!!!
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 510;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

int a[N], b[N], n, x;

int main()
{
#ifdef OlaMins
	freopen("D:/input.txt", "r", stdin);
	//freopen("D:/output.txt", "w", stdout);
#endif

	int T; cin >> T;
	while (T--) {
		sc("%d %d", &n, &x);
		for (int i = 1; i <= n; i++)
			sc("%d", &a[i]);
		if (is_sorted(a + 1, a + n + 1)) {
			puts("0");
			continue;
		}

		int ans = INF;
		for (int s = 1; s <= n; s++) { // 枚举x在哪个位置
			MPY(b, a);
			if (b[s] <= x)
				continue;
			int t = x, tot = 1;
			swap(b[s], t);

			for (int i = s + 1; i < n; i++) {
				if (is_sorted(b + i, b + n + 1))
					break;
				if (b[i] > t)
					swap(b[i], t), tot++;
			}

			if (is_sorted(b + 1, b + n + 1))
				Min(ans, tot);
		}
		if (ans == INF)
			puts("-1");
		else
			printf("%d\n", ans);
	}
	return 0; // 改数组大小!!!用pair改宏定义!!!
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 1e3 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

int a[N], b[N], n, x, mx;

void Init() {
	for (int i = 0; i <= n + 5; i++)
		b[i] = 0;
	for (int i = 1; i <= n; i++)
		sc("%d", &a[i]);
	for (int i = 1; i <= n; i++) {
		int j = 1;
		while (j < i) {
			if (a[j] > a[i])
				b[j] = 1;
			j++;
		}
	}
	mx = 0;
}
void Calc() {

	int pos = 1;
	while (b[pos] == 0 && pos <= n)
		pos++;

	for (int j = pos; j >= 1 && pos <= n; j--) {
		if (a[j] > x)
			pos = j, b[j] = 1;
	}

	while (pos <= n && a[pos] >= x) {
		if (a[pos] != x) {
			mx++;
			int t = a[pos];
			a[pos] = x, x = t;
			int nxt = pos + 1;
			while (b[nxt] == 0 && nxt <= n)
				nxt++;
			for (int j = nxt; j >= 1 && j <= n; j--) {
				if (a[j] > x) {
					nxt = max(j, pos + 1);
					b[j] = 1;
				}
			}
			pos = nxt;
		}
		else
			pos++;
	}

}

int main()
{
#ifdef OlaMins
	freopen("D:/input.txt", "r", stdin);
	//freopen("D:/output.txt", "w", stdout);
#endif

	int T; cin >> T;
	while (T--) {
		sc("%d %d", &n, &x);
		Init(), Calc();

		bool ok = true;
		for (int i = 2; i <= n && ok; i++) {
			if (a[i] < a[i - 1])
				ok = false;
		}
		if (ok)
			printf("%d\n", mx);
		else
			puts("-1");
	}
	return 0; // 改数组大小!!!用pair改宏定义!!!
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int n,x,a[maxn];
bool judge(){
	for(int i=2;i<=n;++i){
		if(a[i-1]>a[i])return false;
	}
	return true;
}
void solve(){
	vector<int>ttt;
	int ans=0;
	cin>>n>>x;
	for(int i=1;i<=n;++i){
		cin>>a[i];
		if(a[i]<=x){ttt.push_back(a[i]);}
	}
	for(int i=1;i<ttt.size();++i){
		if(ttt[i-1]>ttt[i]){
			cout<<"-1\n";return ;
		}
	}
	if(judge()){
		cout<<"0\n";
		return;
	}
	for(int i=1;i<=n;++i){
		if(a[i]>x){
			swap(a[i],x);
			ans++;
		}
		if(judge()){break;}
	}
	if(judge()){
		cout<<ans<<"\n";
	}
	else{
		cout<<"-1\n";
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		cout<<"-1\n";
		 		return ;
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
					cout<<"-1\n";
					return ;
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			int temp=x;
		 			x=a[i];
		 			for(int k=i;k>j+1;k--){
		 				a[k]=a[k-1];
		 			}
					a[j+1]=temp;
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		cout<<"-1\n";
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
					cout<<"-1\n";
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		cout<<"-1\n";
		 		return ;
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
					cout<<"-1\n";
					return ;
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		cout<<"-1\n";
		 		return ;
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
					cout<<"-1\n";
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	int pos;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		if(x>a[i]){
		 			swap(a[i+1],x);
		 		}
		 		else{
		 			cout<<"-1\n";
		 			return ;
		 		}
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>=x){j--;}
		 		if(j==i){
		 			if(x>a[i]){
		 				swap(a[i+1],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return;
		 			}
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		if(x>a[i]){
		 			swap(a[i+1],x);
		 		}
		 		else{
		 			cout<<"-1\n";
		 			return ;
		 		}
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
		 			if(x>a[i]){
		 				swap(a[i+1],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return;
		 			}
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		cout<<"-1\n";
		 		return ;
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
					cout<<"-1\n";
					return ;
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 			a[i]=a[i-1];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int n,x;
bool flag=0;
void solve(){
	cin>>n>>x;
	flag=0;
	int ans=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		 if(a[i]>a[i+1]){
		 	if(a[i-1]>a[i+1]){
		 		if(x>a[i]){
		 			swap(a[i+1],x);
		 		}
		 		else{
		 			cout<<"-1\n";
		 			return ;
		 		}
		 	}
		 	else{
		 		int j=i;
		 		while(j>0&&a[j]>x){j--;}
		 		if(j==i){
					cout<<"-1\n";
		 		}
		 		else if(j==i-1){
		 			if(x<=a[i+1]){
		 				swap(a[i],x);
		 				ans++;
		 			}
		 			else{
		 				cout<<"-1\n";
		 				return ;
		 			}
		 		}
		 		else{
		 			ans+=i-j;
		 			x=a[i];
		 		}
		 	}
		 }
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int Case;
	cin>>Case;
	while(Case--){
		solve();
	}
	return 0;
}


///***###                               بسم الله الرحمن الرحيم                            ###***///

/*
                    Author          : Borhan Uddin Rafi
                    University      : Begum Rokeya University,Rangpur.
                    Date            : 01 Dec,2020;
*/

///************************Start Of Template*************************///

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#define Read_Fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define all(a) a.begin(),a.end()
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define f0(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<n;i++)
#define f1n(i,n) for(i=1;i<=n;i++)
#define szf sizeof()
#define sz size()
#define ln length()
#define mem(x,r) memset(x,r,sizeof(x))
#define inf 1000000000000
using namespace std;
using ll=long long;
const int N=1e1+4;

//////graph////////

vector<int>circle;
bool vis[N];
int parent[N][N];
ll chk[N];
int color[N];
int tim[N];
int dis[N];
int position[N];
vector<int>adj[N];
vector<int>adj1[N];
vector<int>graph[N];
bool has_cycle;
int maxdis,maxnode,Totnode,depth=1;
bool ok;
queue<int>q;
stack<int>stk;
vector<int>solution;
int indegree[N];
int go[N];
int to[N];
ll x1,x2,x3,x4,x5,x6;
string ss;
int gn,gk;

//////graph////////

int brr[51][51];
int arr[51][51];

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    //Read_Fast;

    int t=1;
    cin>>t;
    int cas=0;
    while(t--)
    {
        int n,m,i,j,cnt=0,cnt1=0,cnt2=0,even=0,odd=0,len,k,r,l,z=0,x=0,y=0,flag=0,sum=0,tottal=0;
        int a=0,b=0,c=0,d=0,ans=0,rem,quot,zero=0,fst=0,null=0,snd=0,lst=0,rone=0,one=0,pos=0,neg=0,mn=INT_MAX,mx=INT_MIN;

        char ch;

        ////Time/////
        int h1,h2,m1,m2,h;

        ////motion ////
        int velo1,velo2,ac1,ac2,tim,hour,mint,sec;

        /////Graph///
        int node,edge,u,v,cost;
        int bst,wrst;
        double nd,ad,bd,cd,dd,xd,sumd=0.00;
        string str,str1="",str2="",str3="",strstore1="",strstore2="";

        //////Input Node //////

        cin>>n>>x;
        int arr[n+4];
        for(i=0; i<n; i++)
        {
            cin>>arr[i];
        }
        for(i=1; i<n; i++)
        {
            if(arr[i]<arr[i-1])
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            flag=0;
            for(i=0; i<n; i++)
            {
                if(x<arr[i])
                {
                    swap(x,arr[i]);
                    cnt++;
                    flag=1;
                    break;
                }
            }

            if(flag)
            {
                cout<<cnt<<endl;
            }
            else
            {
                cout<<-1<<endl;
            }
        }
        else
        {
            cout<<'0'<<endl;
        }

    }

    return 0;

    //////***###            ٱلْحَمْدُ لِلَّٰهِ          ###***//////

}


///***###                               بسم الله الرحمن الرحيم                            ###***///

/*
                    Author          : Borhan Uddin Rafi
                    University      : Begum Rokeya University,Rangpur.
                    Date            : 01 Dec,2020;
*/

///************************Start Of Template*************************///

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#define Read_Fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define all(a) a.begin(),a.end()
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define f0(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<n;i++)
#define f1n(i,n) for(i=1;i<=n;i++)
#define szf sizeof()
#define sz size()
#define ln length()
#define mem(x,r) memset(x,r,sizeof(x))
#define inf 1000000000000
using namespace std;
using ll=long long;
const int N=1e1+4;

//////graph////////

vector<int>circle;
bool vis[N];
int parent[N][N];
ll chk[N];
int color[N];
int tim[N];
int dis[N];
int position[N];
vector<int>adj[N];
vector<int>adj1[N];
vector<int>graph[N];
bool has_cycle;
int maxdis,maxnode,Totnode,depth=1;
bool ok;
queue<int>q;
stack<int>stk;
vector<int>solution;
int indegree[N];
int go[N];
int to[N];
ll x1,x2,x3,x4,x5,x6;
string ss;
int gn,gk;

//////graph////////

int brr[51][51];
int arr[51][51];

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    //Read_Fast;

    int t=1;
    cin>>t;
    int cas=0;
    while(t--)
    {
        int n,m,i,j,cnt=0,cnt1=0,cnt2=0,even=0,odd=0,len,k,r,l,z=0,x=0,y=0,flag=0,sum=0,tottal=0;
        int a=0,b=0,c=0,d=0,ans=0,rem,quot,zero=0,fst=0,null=0,snd=0,lst=0,rone=0,one=0,pos=0,neg=0,mn=INT_MAX,mx=INT_MIN;

        char ch;

        ////Time/////
        int h1,h2,m1,m2,h;

        ////motion ////
        int velo1,velo2,ac1,ac2,tim,hour,mint,sec;

        /////Graph///
        int node,edge,u,v,cost;
        int bst,wrst;
        double nd,ad,bd,cd,dd,xd,sumd=0.00;
        string str,str1="",str2="",str3="",strstore1="",strstore2="";

        //////Input Node //////

        cin>>n>>x;
        int arr[n+4];
        for(i=0; i<n; i++)
        {
            cin>>arr[i];
        }
        for(i=1; i<n; i++)
        {
            if(arr[i]<arr[i-1])
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            flag=0;
            for(i=0; i<n; i++)
            {
                if(x<arr[i])
                {
                    swap(x,arr[i]);
                    cnt++;
                    flag=1;
                }
            }

            if(flag)
            {
                cout<<cnt<<endl;
            }
            else
            {
                cout<<-1<<endl;
            }
        }
        else
        {
            cout<<'0'<<endl;
        }

    }

    return 0;

    //////***###            ٱلْحَمْدُ لِلَّٰهِ          ###***//////

}