#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  int n;
  cin >> n;
  vector<pair<int,int>> v;
  for(int i = 1; i <= n; i++){
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
  }
  int result = 0;
  for(int i = 1; i < v.size(); i++)
    if(v[i].second < v[0].second)
      swap(v[0], v[i]);
  sort(v.begin() + 1, v.end());

  for(int i = 0; i < v.size(); i++)
    result = max(result, v[i].second - v[i].first + 1);

  for(int i = 1;i < v.size() - 1; i++)
    result = max(result, (v[i].second - v[i].first + 1) + (v[0].second - max(v[0].first, v.back().first) + 1));


  int minr = v.back().second;
  for(int i = v.size() - 1; 1 <= i; i--){
    minr = min(minr, v[i].second);
    result = max(result, (minr - v.back().first + 1) + (v[0].second - max(v[0].first, v[i - 1].first) + 1));
  }
  cout << result;
  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <climits>

using namespace std;

int main() {
	int i, j, k, to, min, temp_result;
	int n;
	int r[100], c[100];
	int result[100][100];
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> r[i] >> c[i];
		result[i][i] = 0;
	}
	
	i = 0;
	j = 1;
	to = n - 2;
	while (j <= n - 1) {
		while (i <= to) {
			//computation of result[i][i+j]
			min = INT_MAX;
			k = i;
			while (k <= i + j - 1) {
				temp_result = c[k] * r[i] * c[i + j] + result[i][k] + result[k + 1][i + j];
				if (temp_result < min) {
					min = temp_result;
				}
				k++;
			}
			result[i][i + j] = min;
			i++;
		}
		i = 0;
		j++;
		to--;
	}
	printf("%d\n", result[0][n - 1]);
}