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

#define prc(n) fixed<<setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define endl "\n"
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define rall(v) v.rbegin(),v.rend()
#define siz(x) (int)x.size()
#define ll long long
#define pii pair<int, int>
const int mod = 998244353;
#define int long long

main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	vector<int>a(n), b(n);
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	int ans=0;
	for(int i=0;i<n;i++){
		int a1 = ((a[i]-1)*a[i] / 2), b1 = (b[i] *(b[i]+1) / 2);
		ans+=(b1 - a1);
	}
	cout<<ans<<"\n";
	return 0;
	
}

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
long long modPow(long long x, long long a) {
  if (a == 1) return x;
  if (a % 2) return (x * modPow(x, a - 1)) % MOD;
  long long t = modPow(x, a / 2);
  return (t * t) % MOD;
}
long long modInv(long long x) {
  return modPow(x, MOD - 2);
  }
  void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0) return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(int *)> f) {
  int indexes[k];
  recursive_comb(indexes, n - 1, k, f);
}

// nPnの順列に対して処理を実行する
void foreach_permutation(int n, std::function<void(int *)> f) {
  int indexes[n];
  for (int i = 0; i < n; i++) indexes[i] = i;
  do {
    f(indexes);
  } while (std::next_permutation(indexes, indexes + n));
}

// nPkの順列に対して処理を実行する
void foreach_permutation(int n, int k, std::function<void(int *)> f) {
  foreach_comb(n, k, [&](int *c_indexes) {
    foreach_permutation(k, [&](int *p_indexes) {
      int indexes[k];
      for (int i = 0; i < k; i++) {
        indexes[i] = c_indexes[p_indexes[i]];
      }
      f(indexes);
    });
  });
}
int main() {
  ll n,q,a,b,c,ans;
  q = 1;
  b = 0; 
  cin >> n;
  ans = n;
  while (n >q)
  {
    a = n/q;
    c = n%q;
    if(a+b+c < ans ){
      ans = a+b+c;
    }
    q *= 2;
    b++;
  }

  cout << ans << endl;
}