#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

long long N;
long long X[1 << 18], C[1 << 18];
long long dp[1 << 18][3];
vector<long long> G[1 << 18];
vector<long long> J;

int main() {
	cin >> N;
	for (int i = 1; i <= N; i++) {
		cin >> X[i] >> C[i];
		J.push_back(C[i]);
	}
	J.push_back(0);
	J.push_back(N + 1);
	sort(J.begin(), J.end());
	J.erase(unique(J.begin(), J.end()), J.end());
	for (int i = 1; i <= N; i++) {
		C[i] = lower_bound(J.begin(), J.end(), C[i]) - J.begin();
		G[C[i]].push_back(X[i]);
	}
	G[0].push_back(0);
	G[J.size() - 1].push_back(0);
	for (int i = 0; i < J.size(); i++) {
		sort(G[i].begin(), G[i].end());
		if (G[i].size() >= 2) {
			vector<long long> H = { G[i][0], G[i][G[i].size() - 1] };
			G[i] = H;
		}
	}

	for (int i = 0; i < J.size(); i++) {
		dp[i][0] = (1LL << 60);
		dp[i][1] = (1LL << 60);
	}

	dp[0][0] = 0;
	for (int i = 1; i < J.size(); i++) {
		for (int j = 0; j < G[i - 1].size(); j++) {
			for (int k = 0; k < G[i].size(); k++) {
				if (G[i].size() == 1) {
					dp[i][k] = min(dp[i][k], dp[i - 1][j] + abs(G[i - 1][j] - G[i][k]));
				}
				if (G[i].size() == 2) {
					dp[i][k] = min(dp[i][k], dp[i - 1][j] + abs(G[i - 1][j] - G[i][k ^ 1]) + abs(G[i][k ^ 1] - G[i][k]));
				}
			}
		}
	}
	cout << dp[J.size() - 1][0] << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
std::mt19937 rnd(time(0));
inline int sj(int n)
{
	unsigned int x=rnd();
	return x%n+1;
}
#define rand fst
template<typename typC> void read(register typC &x)
{
	register int c=getchar(),fh=1;
	while ((c<48)||(c>57))
	{
		if (c=='-') {c=getchar();fh=-1;break;}
		c=getchar();
	}
	x=c^48;c=getchar();
	while ((c>=48)&&(c<=57))
	{
		x=x*10+(c^48);
		c=getchar();
	}
	x*=fh;
}
template<typename typC, typename... Args> void read(typC &first, Args& ... args) {
	read(first);
	read(args...);
}
template<typename typC> void read(register typC *a,int num)
{
	for (register int i=1;i<=num;i++) read(a[i]);
}
template<typename typC> void write(register typC x)
{
	if (x<0) putchar('-'),x=-x;
	static int st[100];
	register int tp=1;
	register typC y;st[1]=x%10;x/=10;
	while (x) y=x/10,st[++tp]=x-y*10,x=y;++tp;
	while (--tp) putchar(st[tp]|48);
}
template<typename typC> void write(register typC *a,register int num)
{
	for (register int i=1;i<=num;i++) write(a[i]),putchar(i==num?10:32);
}
template<typename typC> typC ab(register typC x)
{
	if (x<0) return -x;
	return x;
}
#define space(x) write(x),putchar(32)
#define enter(x) write(x),putchar(10)
const int p=1e9+7;
const db eps=1e-9;
inline void inc(register int &x,const int y)
{
	if ((x+=y)>=p) x-=p;
}
inline void dec(register int &x,const int y)
{
	if ((x-=y)<0) x+=p;
}
inline int ksm(register int x,register int y)
{
	register int r=1;
	while (y)
	{
		if (y&1) r=(ll)r*x%p;
		x=(ll)x*x%p;
		y>>=1;
	}
	return r;
}
priority_queue<int> ONLYYFORRCOPYY;
priority_queue<int,vector<int>,greater<int> > ONLYYFORRCOPYY__;
struct Q
{
	int u,v;
	Q(int a=0,int b=0):u(a),v(b){}
	bool operator<(const Q &o) const {return v<o.v;}
};
const int N=1e6+2,M=1e6+2,inf=1e9;
vector<Q> lj[N];
ll lb[N][3];
int a[N],b[N],ds[N],dep[N],cnt[2];
int T,n,m,c,i,j,k,x,y,z,ans,la,ksiz,ks;
bool ed[N];
void dfs(int x)
{
	ed[x]=1;cnt[dep[x]]++;
	for (auto v:lj[x]) if (!ed[v.u])
	{
		dep[v.u]=dep[x]^v.v;
		dfs(v.u);
	}
	ed[x]=0;
}
int main()
{
	//ios::sync_with_stdio(false);
	cout<<setiosflags(ios::fixed)<<setprecision(15);
	T=1;
	read(n);
	for (i=1;i<n;i++) read(lb[i]-1,3),++ds[lb[i][0]],++ds[lb[i][1]];
	for (j=0;j<60;j++)
	{
		cnt[0]=cnt[1]=0;
		for (i=1;i<=n;i++) lj[i].clear(),lj[i].reserve(ds[i]);
		for (i=1;i<n;i++)
		{
			x=lb[i][0];y=lb[i][1];z=lb[i][2]>>j&1;
			lj[x].push_back(Q(y,z));
			lj[y].push_back(Q(x,z));
		}dfs(1);
		ans=(ans+(1ll<<j)%p*cnt[0]%p*cnt[1])%p;
	}
	write(ans);
}
