//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp> 
//#include <ext/pb_ds/tree_policy.hpp> 
//using namespace __gnu_pbds; 
#define int long long 
#define ll int
#define all(a) a.begin(),a.end()    
#define rev(a) a.rbegin(),a.rend()    
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);



void solve()
{
	int n;
	cin>>n;
    vector<vector<int>> same(n+2);
    vector<int> x(n),c(n);
    fi(0,n)
    {
    	cin>>x[i]>>c[i];
    	same[c[i]].pb(x[i]);
    }
    same[n+1]={0,0};
    fi(1,n+1)
    {
    	if(same[i].size()==0) continue;
    	sort(all(same[i]));
    }
    ar<int,2> dp,prev;

    prev[0]=prev[1]=dp[0]=dp[1]=0;
    fk(1,n+2)
    {
    	if(same[k].size()==0) continue;
    	ar<int,2> ndp;
    	ndp[0]=ndp[1]=1e15;
    	fi(0,2)
    	{
	    	if(prev[i]<=same[k][0])
	    	{
	    		// ndp[0]=min(ndp[0],dp[i]+2*(same[k].back()-same[k][0])+same[k][0]-prev[i]);
	    	}
	    	else if(prev[i]>same[k][0]&&prev[i]<=same[k].back())
	    	{
	    		ndp[0]=min(ndp[0],dp[i]+(same[k].back()-same[k][0])+same[k].back()-prev[i]);
	    	}
	    	else
	    	{
	    		ndp[0]=min(ndp[0],dp[i]+(same[k].back()-same[k][0])+prev[i]-same[k].back());
	    	}
	    }
	    fi(0,2)
    	{
	    	if(prev[i]<=same[k][0])
	    	{
	    		ndp[1]=min(ndp[1],dp[i]+(same[k].back()-same[k][0])+same[k][0]-prev[i]);
	    	}
	    	else if(prev[i]>same[k][0]&&prev[i]<=same[k].back())
	    	{
	    		ndp[1]=min(ndp[1],dp[i]+(same[k].back()-same[k][0])+prev[i]-same[k][0]);
	    	}
	    	else
	    	{
	    		// ndp[1]=min(ndp[1],dp[i]+2*(same[k].back()-same[k][0])+prev[i]-same[k].back());
	    	}
	    }
	    // if(same[k].size()==1)
	    // {
	    // 	assert(ndp[0]==ndp[1]);
	    // }
    	dp=ndp;
    	prev[0]=same[k][0];
    	prev[1]=same[k].back();
    }
    cout<<min(dp[0],dp[1])<<'\n';

    
    
}

signed main()
{
    FAST;
    int tt=1;
    //cin>>tt;    
    while(tt--)
    {
        solve();
    }    
}










//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1).
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)

//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
//#include <x86intrin.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define PI 3.141592653589793L
#define FAST ios::sync_with_stdio(false); cin.tie(NULL);
// Use for file I/O;
#define FIN string _fname = "input"; \
			string _is = _fname + ".in", _os = _fname + ".out"; \
			freopen(_is.c_str(), "r", stdin); \
			freopen(_os.c_str(), "w", stdout);

#define FIN2 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define RINIT mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define gsize(x) (int)((x).size())

using namespace std;
//using namespace __gnu_pbds;
//typedef gp_hash_table<int, int, hash<int>> ht;

const char nl = '\n';
const char Nl = '\n';

typedef long long ll;
typedef long double ld;
// typedef unsigned int uint;
typedef unsigned long long ull;

const ll inf = 1e9 + 10;
const ll inf2 = 1e18 + 99LL;
const ld inf3 = 1e14;
const ll mod = 1e9+7, mod2 = 998244353;
const ld eps = 1e-2;
const bool local = false;

const int logn = 8, maxn = 400005, maxm = 10001, maxn2 = 3;

int main() {
    FAST; //RINIT;
    
    int n;
    cin >> n;
    vector<ll> p(n + 1);
    for (int i = 0; i < n; i++) {
		ll a;
		cin >> a;
		p[i + 1] = p[i];
		if (i&1) p[i + 1] += a;
		else p[i + 1] -= a;
	}
	
	map<ll, int> ct;
	
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ct[p[i - 1]]++;
		ans += ct[p[i]];
	}
	cout << ans << nl;
}


// © Benq 
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
