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 Kotlin (1.3.71) (judge verdict: Accepted)

import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.math.*
import kotlin.system.exitProcess

@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

const val _BUFFER_SIZE = 1 shl 16
@JvmField val _buffer = ByteArray(_BUFFER_SIZE)
@JvmField var _bufferPt = 0
@JvmField var _bytesRead = 0

tailrec fun readChar(): Char {
    if(_bufferPt == _bytesRead) {
        _bufferPt = 0
        _bytesRead = INPUT.read(_buffer, 0, _BUFFER_SIZE)
    }
    return if(_bytesRead < 0) Char.MIN_VALUE
    else {
        val c = _buffer[_bufferPt++].toChar()
        if (c == '\r') readChar()
        else c
    }
}

fun readLine(): String? {
    var c = readChar()
    return if(c == Char.MIN_VALUE) null
    else buildString {
        while(true) {
            when(c) {
                '\n', Char.MIN_VALUE -> return@buildString
                else -> {
                    append(c)
                    c = readChar()
                }
            }
        }
    }
}
fun readLn() = readLine()!!

fun read() = buildString {
    var c = readChar()
    while(c <= ' ') {
        if(c == Char.MIN_VALUE) return@buildString
        c = readChar()
    }
    do {
        append(c)
        c = readChar()
    } while(c > ' ')
}
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

fun main() {

    val k = readInt()

    var mod = 0
    var ok = false
    for (i in 0 until 10_000_000) {
        mod = (10 * mod + 7) % k
        if (mod == 0) {
            println(i + 1)
            ok = true
            break
        }
    }

    if (!ok) {
        println(-1)
    }

//    val (n, queryCount) = readIntArray(2)
//    val a = readIntArray(n)
//
//    val segment = Array(4*n) { mutableSetOf<Int>()}
//
//    fun build(v: Int, left: Int, right: Int) {
//        if (left == right) {
//            segment[v].add(a[left])
//        } else {
//            val mid = left + (right - left) / 2
//
//            val s1 = build(2*v + 1, left, mid)
//            val s2 = build(2*v + 2, mid + 1, right)
//            segment[v].addAll(segment[2*v + 1])
//            segment[v].addAll(segment[2*v + 2])
//        }
//    }
//
//    build(0, 0, n - 1)
//
//    fun query(v: Int, left: Int, right: Int, queryLeft: Int, queryRight: Int): Set<Int> {
//        if (left == queryLeft && right == queryRight) {
//            return segment[v]
//        }
//
//        val mid = left + (right - left) / 2
//        if (queryRight <= mid) {
//            return query(2 * v + 1, left, mid, queryLeft, queryRight)
//        }
//
//        if (queryLeft > mid) {
//            return query(2* v + 2, mid + 1, right, queryLeft, queryRight)
//        }
//
//        val result = mutableSetOf<Int>()
//        result.addAll(query(2*v + 1, left, mid, queryLeft, mid))
//        result.addAll(query(2*v + 2, mid + 1, right, mid + 1, queryRight))
//        return result
//    }
//
//    for (query in 0 until queryCount) {
//        val (a, b) = readIntArray(2)
//        println(query(0, 0, n -1, a - 1, b - 1).size)
//    }
}