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: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	int x=n%10;
	if(x==2||x==4||x==5||x==6||x==8||x==0)
	cout<<-1<<endl;
	else
	{
		vector<int> v(1000000,0);
		int cur=0,mx=1;
		while(1)
		{
			int xx;
			if(v[cur]<=7)
			xx=7-v[cur];
			else
			xx=17-v[cur];
			if((xx==0&&cur==mx)||(xx==7&&cur==mx+1))
			{
				if(xx==0)
				cur++;
			break;}
			int i;
			for(i=1;i<=9;i++)
			{
				if((x*i)%10==xx)
				break;
			}
			int m=n,c=cur;
			mx=max(mx,cur);
			while(m>0)
			{
				int y=m%10;
				m/=10;
				int j=y*i;
				int xy=(v[c]+j%10)/10;
				v[c]=(v[c]+j%10)%10;
				xy+=j/10;
				int c1=c+1;
				while(xy>0)
				{
					int r=(v[c1]+xy)/10;
					v[c1]=(v[c1]+xy)%10;
					mx=max(mx,c1);
					c1++;
					xy=r;
				}
				c++;
			}
			cur++;
		}
		cout<<cur<<endl;
	}
	return 0;
}