Divisible by 1000003? Easy, just multiply the last digit by 300001 and add! Given a prime P greater than 10, your program or function must figure out its divisibility rule x, defined as the integer with smallest absolute value which yields a multiple of the original prime when multiplied by the last digit of the prime and added to the rest of the original prime.

Example

Given an input 31, the last digit is 1 and the rest of the number is 3. Thus your program must find the integer x with minimum absolute value such that 1*x + 3 is a multiple of 31. In this case, x=-3 works, so the program or function would return -3.

Given an input 1000003, the last digit is 3 and the rest of the number is 100000. Thus your program would find x=300001 because 3*300001+100000 = 1000003 which is a multiple of 1000003.

Mathematical Background

The value of x can be used as a divisibility test. If a number N is divisible by P, then adding x times the last digit of N to the rest of N will yield a multiple of P if and only if N is divisible by P in the first place.

For P=11, we get x=-1, which is equivalent to the well-known divisibility rule for 11: a number is divisible by 11 alternating difference of its digits is divisible by 11.

Rules


The output may be in any form that clearly encodes both the sign and value of the output.
The input prime will be between 10 and 2^30.
You do not need to handle if the input is not a prime or is not in the range.
You do not need to handle if both x and -x are valid outputs (should not happen).
Brute force is permitted, but more creative solutions are appreciated.
This is code-golf, so shortest code in each language wins! Do not let answers in golfing languages discourage you from posting in other languages.


Test Cases

Input   Output
11  -1
13  4
17  -5
19  2
23  7
29  3
31  -3
37  -11
41  -4
43  13
47  -14
53  16
59  6
61  -6
67  -20
71  -7
73  22
79  8
83  25
89  9
97  -29
101 -10
103 31
107 -32
109 11
113 34
127 -38
131 -13
1000003 300001
2000003 600001
2999999 300000
9999991 -999999
JavaScript (ES6), 32 25 23 bytes



f=
n=&gt;(3/(n%5*2-5)*n+1)/10
&lt;input type=number min=1 oninput=o.textContent=this.value%5*(this.value%2)?f(this.value):``&gt;&lt;pre id=o&gt;




3/(n%5*2-5) would be written 9/n(mod -10) if I had access to balanced modulo division. Edit: Saved 2 bytes thanks to @EgorSkriptunoff