Problem: There is a frog known as "CHAMELEON" because he has a special feature to change its body's color similar to stone's color on which he sits. There are N colorful stones lying in a row, but of only 1 to 100 different colors. Frog can hopp on another stone if the stone has same color as its body or (i-1)th stone if it is currently on ith stone. Frog needs to hopp from position 'S' to 'E' via 'M'. Finds the minimum no. of jumps he needs to take to reach E from S via M.

INPUT:

First line contain a integer N. Next line contain N stones(0- based index) each ith stone is denoted by a number which is color of the stone.
Next line contain Q (no. of queries). Each query contain S,M,E.

3 ≤ N ≤ 10^5.

1 ≤ Q ≤ 50

0 ≤ S,M,E<N 

OUTPUT:

Answer of each query. Print -1 if it will not able to reach the destination point.

SAMPLE INPUT
6
2 3 1 3 2 4
2
1 0 4
2 3 5

SAMPLE OUTPUT
2
-1

Solution (in PYTHON):

Solution:
from collections import deque
class Frog():
	
	def __init__(self,n,stones):
		self.colors=[[] for _ in xrange(101)]
		self.N = n
		self.stones = stones
		for i,c in enumerate(stones):
			self.colors[c].append(i)
		self.l=[0]*self.N
		
		
			
	def bfsThrough(self,S,M,E):
		s1 = self.bfs(S,M)
		if s1==-1:
			return s1
		s2 = self.bfs(M,E)
		if s2==-1:
			return s2
		return s1+s2
		
	def bfs(self,A,B):
		if A==B:
			return 0
		v=[False]*self.N
		f=[False]*101
		#l=[0]*self.N
		
		q=deque()
		v[A]=True
		q.append(A)
		self.l[A]=0
		while(len(q)>0):
			cur = q.popleft()
			
			if not f[self.stones[cur]]:
				f[self.stones[cur]]=True
				for x in self.adjacentTo(cur):
					if x==B:
						return self.l[cur]+1
					if not v[x]:
						v[x]=True
						q.append(x)
						self.l[x]=self.l[cur]+1
			
			if cur>0:
				x=cur-1
				if x==B:
					return self.l[cur]+1
				if not v[x]:
					v[x]=True
					q.append(x)
					self.l[x]=self.l[cur]+1
				
		
		return -1
		
	def adjacentTo(self,v):
		return self.colors[self.stones[v]]
		
		
def main():
	N = int(raw_input())
	st = [int(x) for x in raw_input().split()]
	f = Frog(N,st)
	for _ in xrange(int(raw_input())):
		S,M,E=[int(x) for x in raw_input().split()]
		print f.bfsThrough(S,M,E)
	
main()