Problem:
Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
#include<bitset>
#include<cstdlib>
#include<set>
#include<unordered_map>
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define low(x) (x&(-x))
#define IL inline
#define reg register
#define LL long long
#define N 500010
#define MO 1000000007
#define INF 0x3f3f3f3f
using namespace std;

LL T,n,i,j,k,x,y,z;
LL a[N],b[N];
char c[N];

map <LL,LL> M;

IL LL Abs(LL x){return (x<0)?-x:x;}
IL void Swap(LL &a,LL &b){a^=b^=a^=b;}
IL LL Min(LL a,LL b){return (a<b)?a:b;}
IL LL Max(LL a,LL b){return (a>b)?a:b;}

IL LL Mi(LL x,LL y,LL MOD){
	LL p=x,t=1,Res=1;
	for (;t<=y;(t&y)?Res=(Res*p)%MOD:0,p=(p*p)%MOD,t<<=1);
	return Res;
}

IL LL read(){
	LL p=0,f=1;	char	c=getchar();
	while (c<48||c>57)	{if (c=='-')	f=-1;	c=getchar();}
	while (c>=48&&c<=57)	p=(p<<1)+(p<<3)+c-48,c=getchar();
	return p*f;
}

LL Gcd(LL a,LL b){return (!b)?a:Gcd(b,a%b);}

inline LL Ex_BSGS(LL x,LL y,LL p){
	register LL i=0,sum=1,s=1,m=0,k=0;
	M.clear();
	while ((s=Gcd(x,p))!=1){
		if (y%s)	return -1;
		sum=sum*(x/s)%p;
		y/=s;	p/=s;	k++;
		if (y==sum)	return k;
	}
	m=(LL)sqrt(p)+1;
	for (i=0;i<=m;i++)	M[y]=i,y=y*x%p;
	s=Mi(x,m,p);	y=s*sum%p;
	for (i=1;i<=m+1;i++){
		if (M[y])	return k+i*m-M[y];
		y=y*s%p;
	}
	return -1;
}

int main(){
	#ifdef __Marvolo
	freopen("zht.in","r",stdin);
	freopen("zht.out","w",stdout);
	#endif
	n=read();
	if (n==7 || n==1){
		puts("1");
		return 0;
	}
	if ((n%7) == 0)	n/=7;
	if ((n%2) == 0){
		puts("-1");
		return 0;
	}
	n*=9;
	x=10;
	for (i=1;i<=4*n;i++){
		if (x%n == 1){
			cout<<i<<endl;
			return 0;
		}
		x=((x*10))%n;
	}
	cout<<"-1"<<endl;
	return 0;
}