#include <bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define pll pair<ll,ll>
#define pint pll
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main() {
  int n;
  cin >> n;
  string s,t;
  cin >> s;
  cin >> t;
  vector<int>v,v2;
  rep(i,n){
    if(s[i]=='1')v.push_back(i);
    if(t[i]=='1')v2.push_back(i);
  }
  ll cnt=v.size(),cnt2=v2.size();
  if(abs(cnt-cnt2)%2 || cnt2>cnt){
    cout << -1 << endl;
    return 0;
  }
  bool used[cnt]={};
  ll ans=0;
  int j=0;
  rep(i,cnt){
    if(used[i])continue;
    if(i+1<cnt && j<cnt2 && v[i]<v2[j]){
      ans+=v[i+1]-v[i];
      used[i+1]=true;
    }
    else if(j<cnt2 && v[i]>v2[j]){
      ans+=v[i]-v2[j];
      j++;
    }
    else if(j<cnt2 && v[i]==v2[j]){
      j++;
    }
    else if(j==cnt2 && i+1<cnt){
      ans+=v[i+1]-v[i];
      used[i+1]=true;
    }
  }
  if(j!=cnt2){
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}

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

#define int long long
#define FOR(i, size) for (int i = 0; i < size; i++)
#define all(x) x.begin(), x.end()
#define endl "\n"
#define MOD 1000000007

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template <typename... Args>
void logger(string vars, Args &&...values)
{
	cout << vars << " = ";
	string delim = "";
	(..., (cout << delim << values, delim = ", "));
}

template <typename T>
void print_list(vector<T> collection, bool newline = true)
{
	for (const auto &item : collection)
	{
		cout << item << " ";
	}
	if (newline)
		cout << endl;
}

#define ONE_TESTCASE 1

int n, m, timer;

void Solve()
{
	cin >> n;
	vector<pair<double, double>> v;
	FOR(i, n)
	{
		int t;
		cin >> t;

		double a, b;
		cin >> a >> b;

		if (t == 2)
		{
			b -= 0.1;
		}
		else if (t == 3)
		{
			a += 0.1;
		}
		else if (t == 4)
		{
			a += 0.1;
			b -= 0.1;
		}

		v.push_back({a, b});
	}

	int cnt = 0;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if ((v[i].second >= v[j].first and v[i].first <= v[j].first) or (v[j].second >= v[i].first and v[i].first >= v[j].first))
				cnt++;
		}
	}
	cout << cnt;
}

int32_t main()
{

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

#if ONE_TESTCASE
	int test = 1;
#else
	int test;
	cin >> test;
#endif

	for (int t = 1; t <= test; t++)
	{
		Solve();
		cout << endl;
	}
}
