1 #include<iostream>
2
3 #include<cstdio>
4
5 using namespace std;
6
7 int a[100001],n,v[100001]= {0},ans[100001],q[100001];
8
9 void bfs()
10
11 {
12
13 q[1]=a[1];
14
15 v[a[1]]=1;
16
17 ans[a[1]]=1;
18
19 int head=1,tail=1;
20
21 while(head<=tail)
22
23 {
24
25 int u=q[head];
26
27 if(u==n) return;
28
29 if(!v[a[u]])
30
31 {
32
33 q[++tail]=a[u];
34
35 ans[a[u]]=ans[u]+1;
36
37 v[a[u]]=1;
38
39 }
40
41 if(u<n&&!v[u+1])
42
43 {
44
45 q[++tail]=u+1;
46
47 ans[u+1]=ans[u]+1;
48
49 v[u+1]=1;
50
51 }
52
53 if(u>0&&!v[u-1])
54
55 {
56
57 q[++tail]=u-1;
58
59 ans[u-1]=ans[u]+1;
60
61 v[u-1]=1;
62
63 }
64
65 ++head;
66
67 }
68
69
70
71 }
72
73 int main()
74
75 {
76
77 cin>>n;
78
79 if(n==1)
80 {
81 cout<<"0"<<endl;
82 return 0;
83 }
84
85 for(int i=1; i<=n; i++)
86
87 cin>>a[i];
88
89 bfs();
90
91 cout<<ans[n];
92
93 return 0;
94
95 }