import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 def Dijkstra(s, graph): INF = 2 ** 63 - 1 import heapq n = len(graph) dist = [INF] * n dist[s] = 0 bef = [0] * n bef[s] = s hq = [(0, s)] heapq.heapify(hq) while hq: c, now = heapq.heappop(hq) if c > dist[now]: continue for to, cost in graph[now]: if dist[now] + cost < dist[to]: dist[to] = cost + dist[now] bef[to] = now heapq.heappush(hq, (dist[to], + to)) return dist, bef def DijkstraRest(bef, t): now = t ret = [] while bef[now] != now: ret.append((bef[now], now)) now = bef[now] ret.reverse() return ret n = ii() a = li() graph = [[] for _ in range(n)] for i in range(n): if 0 <= i - 1: graph[i].append((i - 1, abs(a[i] - a[i - 1]) - 1)) if i + 1 < n: graph[i].append((i + 1, abs(a[i] - a[i + 1]) - 1)) d, _ = Dijkstra(0, graph) print(d[n - 1] + a[0] - 1)