#include <bits/stdc++.h>
using namespace std;
const long long nmax = (long long)2e5 + 10;
long long n;
long long black[nmax];
long long green[nmax];
long long ans[nmax] = {0};
void calcSeg(long long beg, long long mn) {
  long long last = beg;
  ans[beg] = mn;
  for (long long i = last + 1; i < n; i++) {
    long long expect = mn + (i - last);
    long long actual = black[i] + green[i];
    if (actual >= expect) {
      ans[i] = expect;
    } else {
      long long last1 = i;
      long long mn1 = actual;
      ans[last1] = mn1;
      for (long long j = last1 - 1; j >= 0; j--) {
        long long expect1 = mn1 + (last1 - j);
        long long actual1 = ans[j];
        if (expect1 < actual1) {
          ans[j] = expect1;
        } else {
          break;
        }
      }
      calcSeg(last1, mn1);
      break;
    }
  }
}
void calcSeg1(long long beg, long long mn) {
  long long last = beg;
  ans[beg] = mn;
  for (long long i = last - 1; i >= 0; i--) {
    long long expect = mn + (last - i);
    long long actual = black[i] + green[i];
    if (actual >= expect) {
      ans[i] = expect;
    } else {
      long long last1 = i;
      long long mn1 = actual;
      ans[last1] = mn1;
      for (long long j = last1 + 1; j < n; j++) {
        long long expect1 = mn1 + (j - last1);
        long long actual1 = ans[j];
        if (expect1 < actual1) {
          ans[j] = expect1;
        } else {
          break;
        }
      }
      calcSeg1(last1, mn1);
      break;
    }
  }
}
signed main() {
  cin >> n;
  long long mn = (long long)1e9 + 7;
  ;
  long long idx = -1;
  for (long long i = 0; i < n; i++) {
    cin >> black[i] >> green[i];
    if (black[i] + green[i] < mn) {
      mn = black[i] + green[i];
      idx = i;
    }
  }
  calcSeg(idx, mn);
  calcSeg1(idx, mn);
  long long answer = 0;
  bool ok = true;
  for (long long i = 0; i < n; i++) {
    long long res = ans[i] - black[i];
    if (res < 0) ok = false;
    answer += res;
  }
  for (long long i = 0; i < n - 1; i++) {
    if (abs(ans[i] - ans[i + 1]) <= 1) {
    } else {
      cout << ans[i] << " " << ans[i + 1] << endl;
      cout << black[i] << " " << black[i + 1] << endl;
      cout << green[i] << " " << green[i + 1] << endl;
      return 0;
    }
  }
  if (!ok)
    cout << "-1" << endl;
  else {
    cout << answer << endl;
    for (long long i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
