#include <iostream>
using namespace std;
#include <bits/stdc++.h>
#include<vector>
int main() {
	// your code goes here
	vector<int>arr={5,6,7,8,10,4,3,2,1};
	int k=8;
	int n=arr.size();
	int count=0;
	int smallest_length=1000;
	unordered_map<int,int>map;
	for(int j=0;j<n;j++)
	 {
	 	
	 	if(map.find(k-arr[j])!=map.end())
	 	 {
	 	 	int length=j-map[arr[j]]+1;
	 	 	smallest_length=min(length,smallest_length);
	 	 }
	 	
	 	map[arr[j]]=j;
	 }
	 if(smallest_length>=2)
	 cout<<smallest_length;
	 else
	  cout<<-1;
	return 0;
}