Chef loves games! But he likes to invent his own. Now he plays game "Digit Jump". Chef has a sequence of digits $S_{1}, S_{2}, \ldots , S_{N}$. He is staying in the first digit $S_{1}$ and wants to reach the last digit $S_{N}$ in the minimal number of jumps. 
While staying in some index $i$ Chef can jump into $i - 1$ and $i + 1$, but he can't jump out from sequence. Or he can jump into any digit with the same value $S_i$. 
Help Chef to find the minimal number of jumps he need to reach digit $S_{N}$ from digit $S_1$. 

-----Input-----
Input contains a single line consist of string $S$ of length $N$ - the sequence of digits.

-----Output-----
In a single line print single integer - the minimal number of jumps he needs.

-----Constraints-----
- $1\leq N \leq 10^5$
- Each symbol of $S$ is a digit from $0$ to $9$. 

-----Example Input 1-----
01234567890

-----Example Output 1-----
1

-----Example Input 2-----
012134444444443

-----Example Output 2-----
4

-----Explanation-----
Test Case 1: Chef can directly jump from the first digit (it is $0$) to the last (as it is also $0$).
Test Case 2: Chef should follow the following path: $1 - 2 - 4 - 5 - 15$.

Difficulty: interview

Starter code:

Solutions:
# cook your dish here
from sys import stdin, stdout
import math
# from itertools import permutations, combinations
from collections import defaultdict,deque
# import bisect
# import heapq as hq

def bfs(d,n,d1,s):
 high=[-1 for x in range(n+1)]
 q=deque([0])
 high[0]=0
 while q:
  node=q.popleft()
  for x in d1[int(s[node])]:
   if high[x]==-1:
    high[x]=high[node]+1
    q.append(x)
  for x in d[node]:
   if high[x]==-1:
    high[x]=high[node]+1
    q.append(x)
 print(high[n])
def main():
 try:
  s=stdin.readline().strip()
  n=len(s)
  d=defaultdict(list)
  
  for i in range(1,len(s)):
   d[int(s[i])].append(i)

  high=[0]*(n+1)
  visit=[False] *(n+1)
  q=deque([0])
  while q:
   node=q.popleft()
   if node==n-1:
    break
   for x in d[int(s[node])]:
    if visit[x]==False:
     visit[x]=True
     q.append(x)
     high[x]=high[node]+1
   d[int(s[node])].clear()
   if node-1>=0 and not visit[node-1]:
    visit[node-1]=True
    q.append(node-1)
    high[node-1]=high[node]+1
   if node+1<n and not visit[node+1]:
    visit[node+1]=True
    q.append(node+1)
    high[node+1]=high[node]+1
  print(high[n-1])
 except:
  pass
 
def add(a, b, c):
 res = a + b;
 if (res >= c):return res - c;
 else:return res;
def mod(a, b, c):
 res = a * b
 if (res >= c):return res % c
 else:return res
def gcd(a, b):
 while b:
  a, b = b, a % b
 return a
def lcm(a, b):
 w = a // gcd(a, b)
 return w * b
def expo(a, b):
 x, y = 1, a
 while (b > 0):
  if (b & 1):
   x = x * y
  y = y * y
  b >>= 1
 return x
def power(a, b, m):
 x, y = 1,
 while (b > 0):
  if (b & 1):x = mod(x, y, m)
  y = mod(y, y, m)
  b >>= 1
 return x
def L():
 return list(map(int, stdin.readline().split()))
def In():
 return list(map(int, stdin.readline().split()))
def I():
 return int(stdin.readline())
P = 1000000007

def __starting_point():
 main()



__starting_point()

from collections import defaultdict
from collections import deque

g=defaultdict(lambda :[])

s=input().strip()
ls=len(s)

vis=[False for i in range(ls)]
dis=[0 for i in range(ls)]

for i in range(1, ls):
 g[s[i]].append(i)


que=deque()

que.append(0)
vis[0]=True

while que:
 indx=que.popleft()

 if indx==ls-1:
  break

 temp=s[indx]

 for i in g[temp]:
  if not vis[i]:
   vis[i]=True 
   que.append(i)
   dis[i]=dis[indx]+1 

 g[temp].clear()
 
 if indx-1>=0 and not vis[indx-1]:
  vis[indx-1]=True
  que.append(indx-1)
  dis[indx-1]=dis[indx]+1 

 if indx+1<ls and not vis[indx+1]:
  vis[indx+1]=True
  que.append(indx+1)
  dis[indx+1]=dis[indx]+1 


print(dis[-1])

from collections import defaultdict
a=input().strip()
n=len(a)
g=defaultdict(list)
for i in range(n):
 g[a[i]].append(i)
vis={}
vis[0]=0
q=[0]
while len(q)!=0:
 tmp=q.pop(0)
 if tmp==n-1:
  break 
 val=a[tmp]
 s=len(g[val])
 for i in range(s):
  if g[val][i] not in vis:
   vis[g[val][i]]=vis[tmp]+1 
   q.append(g[val][i])
 g[val]=[]
 if tmp+1<=n-1 and tmp+1 not in vis:
  q.append(tmp+1)
  vis[tmp+1]=vis[tmp]+1 
 if tmp-1>=0 and tmp-1 not in vis:
  q.append(tmp-1)
  vis[tmp-1]=vis[tmp]+1
print(vis[n-1])
 
   


from collections import defaultdict,deque
text=input().strip()
v=len(text)
g=defaultdict(list)
store=defaultdict(list)
for i in range(1,v):
 temp=ord(text[i])
 store[temp].append(i) 
que=[0]
dist=[0]*v
vis=[False]*v
vis[0]=True
while(len(que)):
 index=que.pop(0)
 if index==v-1:
  break
 temp=ord(text[index])
 for i in store[temp]:
  if not vis[i]:
   vis[i]=True
   que.append(i)
   dist[i]=dist[index]+1
 del store[temp]
 left=index-1
 right=index+1
 if left>=0 and not vis[left]:
  vis[left]=True
  que.append(left)
  dist[left]=dist[index]+1
 if right<v and not vis[right]:
  vis[right]=True
  que.append(right)
  dist[right]=dist[index]+1
print(dist[v-1])

from collections import defaultdict
from collections import deque

g=defaultdict(lambda :[])

s=input().strip()
ls=len(s)

vis=[False for i in range(ls)]
dis=[0 for i in range(ls)]

for i in range(1, ls):
 g[s[i]].append(i)


que=deque()

que.append(0)
vis[0]=True

while que:
 indx=que.popleft()

 if indx==ls-1:
  break

 temp=s[indx]

 for i in g[temp]:
  if not vis[i]:
   vis[i]=True 
   que.append(i)
   dis[i]=dis[indx]+1 

 g[temp].clear()
 
 if indx-1>=0 and not vis[indx-1]:
  vis[indx-1]=True
  que.append(indx-1)
  dis[indx-1]=dis[indx]+1 

 if indx+1<ls and not vis[indx+1]:
  vis[indx+1]=True
  que.append(indx+1)
  dis[indx+1]=dis[indx]+1 


print(dis[-1])

from collections import defaultdict
from collections import deque

s = input().strip()
n = len(s)
dist = [0]*(n)
visited = [False]*n
d = defaultdict(list)
for i in range(n):
 d[s[i]].append(i)
visited[0] = True
stk = deque()
stk.append(0)
while stk:
 node = stk.popleft()
 if node == n-1:
  break
 for i in d[s[node]]:
  if not visited[i] and i != node:
   dist[i] = dist[node]+1
   visited[i] = True
   stk.append(i)
 d[s[node]] = []
 if node > 0 and not visited[node-1]:
  stk.append(node-1)
  visited[node-1] = True
  dist[node-1] = dist[node]+1
 if node < n-1 and not visited[node+1]:
  stk.append(node+1)
  visited[node+1] = True
  dist[node+1] = dist[node]+1
# print(d)
# print(dist)
print(dist[-1])


from collections import defaultdict
a=input().strip()
n=len(a)
g=defaultdict(list)
for i in range(n):
 g[a[i]].append(i)
vis={}
vis[0]=0
q=[0]
while len(q)!=0:
 tmp=q.pop(0)
 if tmp==n-1:
  break 
 val=a[tmp]
 s=len(g[val])
 for i in range(s):
  if g[val][i] not in vis:
   vis[g[val][i]]=vis[tmp]+1 
   q.append(g[val][i])
 g[val]=[]
 if tmp+1<=n-1 and tmp+1 not in vis:
  q.append(tmp+1)
  vis[tmp+1]=vis[tmp]+1 
 if tmp-1>=0 and tmp-1 not in vis:
  q.append(tmp-1)
  vis[tmp-1]=vis[tmp]+1
print(vis[n-1])
 
   


from sys import stdin, stdout

S = [int(z) for z in stdin.readline().strip()]
n = len(S)
graph = {}
d = {}
visited = {}
for i in range(n):
 graph[S[i]] = []
 visited[i] = 0
 d[i] = 0
if n>1:
 for i in range(n):
  graph[S[i]].append(i)
 queue = [0]

 while len(queue) != 0:
  u = queue.pop(0)
  for v in graph[S[u]]:
   if visited[v] == 0 and v!=u:
    d[v] = d[u] + 1
    queue.append(v)
    visited[v] = 1
  graph[S[u]] = []
  if u-1>=0:
   if visited[u-1] == 0:
    d[u-1] = d[u] + 1
    visited[u-1] = 1
    queue.append(u-1)
  if u+1<n:
   if visited[u+1] == 0:
    d[u+1] = d[u] + 1
    visited[u+1] = 1
    queue.append(u+1)

  visited[u] = 1
 stdout.write("%d\n"%d[n-1])
else:
 stdout.write("0\n")

from sys import stdin, stdout

S = [int(z) for z in stdin.readline().strip()]
n = len(S)
graph = {}
d = {}
visited = {}
for i in range(n):
 graph[S[i]] = []
 visited[i] = 0
 d[i] = 0
if n>1:
 for i in range(n):
  graph[S[i]].append(i)
 queue = [0]

 while len(queue) != 0:
  u = queue.pop(0)
  for v in graph[S[u]]:
   if visited[v] == 0 and v!=u:
    d[v] += d[u] + 1
    queue.append(v)
    visited[v] = 1
  graph[S[u]] = []
  if u-1>=0:
   if visited[u-1] == 0:
    d[u-1] = d[u] + 1
    visited[u-1] = 1
    queue.append(u-1)
  if u+1<n:
   if visited[u+1] == 0:
    d[u+1] = d[u] + 1
    visited[u+1] = 1
    queue.append(u+1)

  visited[u] = 1
 stdout.write("%d\n"%d[n-1])
else:
 stdout.write("0\n")

from collections import defaultdict

def solve():

 a = input().strip()
 n = len(a)
 #print(n)
 g = defaultdict(list)
 for i in range(n):
  g[a[i]].append(i)
 vis = {}
 vis[0] = 0
 q = [0]
 # print(g)
 while len(q)!=0:
  tmp = q.pop(0)
  if tmp==n-1:
   break
  val = a[tmp]
  x = len(g[val])
  for i in range(x):
   if g[val][i] not in vis:
    q.append(g[val][i])
    vis[g[val][i]] = vis[tmp]+1
  g[val] = []
  if tmp+1<=n-1 and tmp+1 not in vis:
   q.append(tmp+1)
   vis[tmp+1] = vis[tmp]+1
  if tmp-1>=0 and tmp-1 not in vis:
   q.append(tmp-1)
   vis[tmp-1] = vis[tmp]+1
 print(vis[n-1])


def __starting_point():
 solve()
__starting_point()

from collections import defaultdict,deque
text=input().strip()
v=len(text)
g=defaultdict(list)
store=defaultdict(list)
for i in range(1,v):
 temp=ord(text[i])
 store[temp].append(i) 
que=deque([0])
dist=[0]*v
vis=[False]*v
vis[0]=True
while(len(que)):
 index=que.popleft()
 if index==v-1:
  break
 temp=ord(text[index])
 for i in store[temp]:
  if not vis[i]:
   vis[i]=True
   que.append(i)
   dist[i]=dist[index]+1
 del store[temp]
 left=index-1
 right=index+1
 if left>=0 and not vis[left]:
  vis[left]=True
  que.append(left)
  dist[left]=dist[index]+1
 if right<v and not vis[right]:
  vis[right]=True
  que.append(right)
  dist[right]=dist[index]+1
print(dist[v-1])

from collections import defaultdict,deque
text=input().strip()
v=len(text)
g=defaultdict(list)
store=defaultdict(list)
for i in range(1,v):
 temp=ord(text[i])
 store[temp].append(i) 
que=[0]
dist=[0]*v
vis=[False]*v
vis[0]=True
while(len(que)):
 index=que.pop(0)
 if index==v-1:
  break
 temp=ord(text[index])
 for i in store[temp]:
  if not vis[i]:
   vis[i]=True
   que.append(i)
   dist[i]=dist[index]+1
 del store[temp]
 left=index-1
 right=index+1
 if left>=0 and not vis[left]:
  vis[left]=True
  que.append(left)
  dist[left]=dist[index]+1
 if right<v and not vis[right]:
  vis[right]=True
  que.append(right)
  dist[right]=dist[index]+1
print(dist[v-1])

# cook your dish here
from collections import defaultdict,deque
text=input().strip()
v=len(text)
g=defaultdict(list)
store=defaultdict(list)
for i in range(1,v):
 temp=ord(text[i])-48
 store[temp].append(i) 
que=[0]
dist=[0]*v
vis=[False]*v
vis[0]=True
while(len(que)):
 index=que.pop(0)
 if index==v-1:
  break
 temp=ord(text[index])-48
 for i in store[temp]:
  if not vis[i]:
   vis[i]=True
   que.append(i)
   dist[i]=dist[index]+1
 del store[temp]
 left=index-1
 right=index+1
 if left>=0 and not vis[left]:
  vis[left]=True
  que.append(left)
  dist[left]=dist[index]+1
 if right<v and not vis[right]:
  vis[right]=True
  que.append(right)
  dist[right]=dist[index]+1
print(dist[v-1])

from collections import defaultdict as dd
s = input().strip()
n = len(s)
vis = [ False for i in range(n)]
d = [ False for i in range(n)]
V = dd(list)
for i in range(1,n):
 val = ord(s[i])-48
 V[val].append(i)
d[0] = 0
vis[0] = True
Q = []
Q.append(0)
while(len(Q)):
 idx = Q[0]
 if(idx == n-1):
  break
 Q.pop(0)
 val = ord(s[idx])-48
 vsz = len(V[val])
 for j in range(vsz):
  nidx = V[val][j]
  if not (vis[nidx]):
   vis[nidx] = True
   Q.append(nidx)
   d[nidx] = d[idx]+1
 V[val].clear()
 if(idx-1 >=0 and not vis[idx-1]):
  vis[idx-1] = True
  Q.append(idx-1)
  d[idx-1] = d[idx] + 1
 if(idx+1<n and not vis[idx+1]):
  vis[idx+1] = True
  Q.append(idx+1)
  d[idx+1] = d[idx] + 1
print(d[n-1])


from collections import defaultdict
class Graph:
 def __init__(self,v):
  self.adj=defaultdict(list)
  self.V = v
 def findstep(self,s):
  visit = [0]*(self.V)
  dist =[0]*self.V
  q = []
  q.append(0)
  while q:
   t = q.pop(0)
   visit[t] =1
   for i in self.adj[s[t]]:
    if visit[i] == 0 and i != t:
      q.append(i)
      visit[i] = 1
      dist[i] = dist[t] + 1
   self.adj[s[t]] = []
   if t > 0 and visit[t-1] == 0:
    q.append(t-1)
    visit[t-1]= 1
    dist[t-1] = dist[t] + 1
   if t < self.V -1 and visit[t+1] == 0:
    q.append(t+1)
    visit[t+1]= 1
    dist[t+1] = dist[t] + 1
  return dist[-1]
s = list(map(int, input().strip()))
n = len(s)
g = Graph(n)
for i in range(n):
 g.adj[s[i]].append(i)
print(g.findstep(s))




from queue import Queue
S = list(map(int, input().strip()))
N = len(S)
graph = {}
for i in range(N):
 graph[S[i]] = []
if N > 1:
 for i in range(N):
  graph[S[i]].append(i)
 visited = [0] * N
 distance = [0] * N
 Q = Queue()
 Q.put(0)
 while (not Q.empty()):
  t = Q.get()
  for i in graph[S[t]]:
   if visited[i] == 0 and i != t:
    Q.put(i)
    visited[i] = 1
    distance[i] = distance[t] + 1
  graph[S[t]] = [] 
  if t > 0:
   if visited[t-1] == 0:
    Q.put(t-1)
    visited[t-1] = 1
    distance[t-1] = distance[t]+1
  if t < N-1:
   if visited[t+1] == 0:
    Q.put(t+1)
    visited[t+1] = 1
    distance[t+1] = distance[t]+1
  visited[t] = 1
 print(distance[-1])
else:
 print(0)

from queue import Queue
S = list(map(int, input().strip()))
N = len(S)
graph = {}
for i in range(N):
 graph[S[i]] = []
if N > 1:
 for i in range(N):
  graph[S[i]].append(i)
 visited = [0] * N
 distance = [0] * N
 Q = Queue()
 Q.put(0)
 while (not Q.empty()):
  t = Q.get()
  for i in graph[S[t]]:
   if visited[i] == 0 and i != t:
    Q.put(i)
    visited[i] = 1
    distance[i] = distance[t] + 1
  graph[S[t]] = [] 
  if t > 0:
   if visited[t-1] == 0:
    Q.put(t-1)
    visited[t-1] = 1
    distance[t-1] = distance[t]+1
  if t < N-1:
   if visited[t+1] == 0:
    Q.put(t+1)
    visited[t+1] = 1
    distance[t+1] = distance[t]+1
  visited[t] = 1
 print(distance[-1])
else:
 print(0)

from sys import stdin, stdout

S = [int(z) for z in stdin.readline().strip()]
n = len(S)
graph = {}
d = {}
visited = {}
for i in range(n):
 graph[S[i]] = []
 visited[i] = 0
 d[i] = 0
if n>1:
 for i in range(n):
  graph[S[i]].append(i)
 queue = [0]

 while len(queue) != 0:
  u = queue.pop(0)
  for v in graph[S[u]]:
   if visited[v] == 0 and v!=u:
    d[v] += d[u] + 1
    queue.append(v)
    visited[v] = 1
  graph[S[u]] = []
  if u-1>=0:
   if visited[u-1] == 0:
    d[u-1] = d[u] + 1
    visited[u-1] = 1
    queue.append(u-1)
  if u+1<n:
   if visited[u+1] == 0:
    d[u+1] = d[u] + 1
    visited[u+1] = 1
    queue.append(u+1)

  visited[u] = 1
 stdout.write("%d\n"%d[n-1])
else:
 stdout.write("0\n")

import heapq

def digitJump(text):
 L = len(text)

 if L == 1 or L == 2:
  return L - 1

 numPos = [ [] for i in range(10) ]
 minJump = [ 2000000 for _ in range(L) ]
 minJump[L-1] = 0

 maxJumpForNum = [ 2000000 for _ in range(10) ]

 for i in range(L):
  numPos[ int(text[i]) ].append(i)
 
 h = []
 heapq.heappush(h, (0, int(text[L-1]), L - 1))

 while len(h) > 0:
  minJ, num, idx = heapq.heappop(h)
  setJump = minJ + 1

  # print(f"minJ: {minJ}  num: {num}  idx: {idx}")

  if maxJumpForNum[num] > setJump:
   maxJumpForNum[num] = setJump

   for i in numPos[num]:
    if i == idx:
     continue
    if minJump[i] > setJump:
     minJump[i] = setJump
     heapq.heappush(h, (setJump, num, i))
   
  if idx > 0:
   if minJump[idx-1] > setJump:
    minJump[idx-1] = setJump
    heapq.heappush(h, (setJump, int(text[idx-1]), idx-1))
  
  if idx < L - 2:
   if minJump[idx+1] > setJump:
    minJump[idx+1] = setJump
    heapq.heappush(h, (setJump, int(text[idx+1]), idx+1))
   
 return minJump[0]
   

def __starting_point():
 text = input().strip()
 print( digitJump(text) )
__starting_point()

"""
-----------------------------Pseudo---------------------------------
"""
import copy
import sys
from collections import defaultdict, Counter

def input(): return sys.stdin.readline()
def mapi(): return map(int,input().split())
def maps(): return map(str,input().split())
#
def print(arg, *argv, end=None):
 sys.stdout.write(str(arg))
 for i in argv: sys.stdout.write(" "+str(i))
 sys.stdout.write(end) if end else sys.stdout.write("\n")
#---------------------------------------------------------------#

def solve():
 t = 1
 #t = int(input())
 while(t):
  t-=1
  a = input().strip()
  n = len(a)
  #print(n)
  g = defaultdict(list)
  for i in range(n):
   g[a[i]].append(i)
  vis = {}
  vis[0] = 0
  q = [0]
  #print(g)
  while len(q)!=0:
   tmp = q.pop(0)
   if tmp==n-1:
    break
   val = a[tmp]
   x = len(g[val])
   for i in range(x):
    if g[val][i] not in vis:
     q.append(g[val][i])
     vis[g[val][i]] = vis[tmp]+1
   g[val] = []
   if tmp+1<=n-1 and tmp+1 not in vis:
    q.append(tmp+1)
    vis[tmp+1] = vis[tmp]+1
   if tmp-1>=0 and tmp-1 not in vis:
    q.append(tmp-1)
    vis[tmp-1] = vis[tmp]+1
  print(vis[n-1])

#---------------------------------------------------------------#
def __starting_point():
 solve()


__starting_point()

from queue import Queue
s = input().strip()
t = [[] for i in range(10)]
for a in range(len(s)):
 b = int(s[a])
 t[b].append(a)
d = [-1]*len(s)
d[0] = 0
q = Queue()
q.put(0)
while q.empty() == False:
 if d[len(s) - 1] != -1:
  break
 h = q.get(0)
 value = int(s[h])
 for c in t[value]:
  if d[c] == -1:
   d[c] = d[h] + 1
   q.put(c)
 t[value] = []
 if h + 1 < len(s)and d[h + 1] == -1:
  d[h + 1] = d[h] + 1
  q.put(h + 1)
 if h - 1 > 0 and d[h - 1] == -1:
  d[h - 1] = d[h] + 1
  q.put(h - 1)

print(d[len(s) - 1])

from queue import Queue
s = input().strip()

#contains a list of some the neibours indexes of a certain node (some are not     counted here : the one with index+1 and index-1)
neighbours = [[] for _ in range(10)]
for index_node in range(len(s)):
 value_node = int(s[index_node])
 neighbours[value_node].append(index_node)
 
#distance of every nodes to the source
distances = [-1]*len(s)
distances[0]=0 #the distance of the source to itself with is 0

#This queue contains the index of the nodes we have not yet visited.
q = Queue()
q.put(0) #initialize the q with the source node

while q.empty()==False :
 if distances[len(s)-1]!=-1:
  break
 #get() is same as the generic pop() operation
 current_node_index = q.get(0)
 value = int(s[current_node_index])
 ### Relaxtion steps 
 ## check most of the neighbours
 for neighbour_index in neighbours[value]:
  #check if this neibhour was not visited before
  if distances[neighbour_index]==-1:
   distances[neighbour_index]=distances[current_node_index]+1
   q.put(neighbour_index)
 neighbours[value] = []
 ## check 2 others neighbours 
 if current_node_index + 1 < len(s)and distances[current_node_index+1]==-1:
  distances[current_node_index+1]=distances[current_node_index]+1
  q.put(current_node_index+1)
 if current_node_index - 1 > 0 and distances[current_node_index-1]==-1:
  distances[current_node_index-1]=distances[current_node_index]+1
  q.put(current_node_index-1)

print(distances[len(s)-1])


#https://www.codechef.com/problems/DIGJUMP
 
import sys
import math
from collections import defaultdict
 
try: 
 sys.stdin = open('input.txt', 'r') 
 sys.stdout = open('output.txt', 'w')
 
except: 
 pass
 

class Graph:
 def __init__(self):
  self.neighbours=defaultdict(set)

 def addEdge(self,u,v):
  if(v not in self.neighbours[u]):
   self.neighbours[u].add(v)
   self.neighbours[v].add(u)

 def BFS(self,n,s,at):
  visited=defaultdict(lambda:False)
  dist=defaultdict(lambda:float("inf"))
  visited[0]=True
  dist[0]=0
  queue=[0]
  added={}
  while queue:
   u=queue.pop(0)
   char=s[u]
   if(char not in added):
    added[char]=True
    for idx in at[char]:
     g.addEdge(u,idx)
   for v in self.neighbours[u]:
    if(not visited[v]):
     visited[v]=True
     queue.append(v)
     dist[v]=dist[u]+1
  return dist[n-1]


 
 
s=input().strip()
at={}
n=len(s)
g=Graph()

at=defaultdict(list)
for i in range(n):
 at[s[i]].append(i)

for i in range(1,n):
 g.addEdge(i-1,i)

for i in range(n-1):
 g.addEdge(i,i+1)


print(g.BFS(n,s,at))




#https://www.codechef.com/problems/DIGJUMP
 
import sys
import math
from collections import defaultdict
 
try: 
 sys.stdin = open('input.txt', 'r') 
 sys.stdout = open('output.txt', 'w')
 
except: 
 pass
 


 
 
s = input().strip()
at={}
n=len(s)
at=defaultdict(list)
for i in range(n):
 at[int(s[i])].append(i)

visited=defaultdict(lambda:False)
dist=defaultdict(lambda:float("inf"))
visited[0]=True
dist[0]=0
queue=[0]
added={}
while queue:
 u=queue.pop(0)
 char=int(s[u])
 for v in at[char]:
  if(not visited[v]):
   visited[v]=True
   queue.append(v)
   dist[v]=dist[u]+1

 at[char]=[]
 if(u!=0):
  v=u-1
  if(not visited[v]):
   visited[v]=True
   queue.append(v)
   dist[v]=dist[u]+1
 if(u!=n-1):
  v=u+1
  if(not visited[v]):
   visited[v]=True
   queue.append(v)
   dist[v]=dist[u]+1

print(dist[n-1])