Sequence Without Sevens This challenge is based on a drinking game. I advise against alcohol consumption while programming.

In this game, the players count up in turns: the first player says 1, the second says 2 and so on. Here's the twist however: each number that is divisible by 7 or has a 7 in its digits (in base 10) is replaced by another counter. This means 7 is replaced by 1, 14 is replaced by 2, 17 is replaced by 3 and so on. For this "sub-sequence", the same rules apply! 35 would be replaced by 7, which in turn is replaced by 1. This process repeats until the number is "valid".

Here's a visualization of the first 100 numbers in the sequence:

n = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
gets replaced by:
                1                  2        3           4                 5  6                    7     8              9             10    11                   12 13                14          15       16 17 18 19 20 21 22 23 24 25             26       27          28                29 30       
gets replaced by:
                                                                                                  1                                                                                   2                       3           4                                   5           6                            

final sequence:
    1 2 3 4 5 6 1 8 9 10 11 12 13  2 15 16  3 18 19 20  4 22 23 24 25 26  5  6 29 30 31 32 33 34  1 36  8 38 39 40 41  9 43 44 45 46 10 48 11 50 51 52 53 54 55 12 13 58 59 60 61 62  2 64 65 66 15 68 69 16  3 18 19 20  4 22 23 24 25 80 81 82 83 26 85 86  5 88 89 90  6 92 93 94 95 96 29 30 99 100


In the final sequence, no number has a seven in its digits or is divisible by seven.

Input description


An integer n (1 &lt;= n &lt;= 7777 or 0 &lt;= n &lt;= 7776)


You may choose whether you use a 0-indexed or 1-indexed input.

Output description


The nth number in the sequence


Examples

The examples are 1-indexed. For 0-indexed, subtract one from n.

n   -&gt;   f(n)
1        1
5        5
7        1
35       1
77       23
119      1
278      86
2254     822
2255     2255
7777     3403


Scoring

This is code-golf, so shortest answer in bytes wins!
Java, 122 121 98 97 bytes

int c(int n){int a=0,b=0,c=n;for(;n--&gt;0;c+=a=((--b+&quot;&quot;).indexOf(55)&amp;b%7)&gt;&gt;31);return a&lt;0?-b:c(c);}

-23 bytes thanks to @Nevay.
-1 byte thanks to @Neil.
Explanation:
Try it here.
int c(int n){                            // Method with integer as parameter and return-type
  int a=0,                               //  Flag integer
      b=0,                               //  Regular counter
      c=n;                               //  7-counter
  for(;n--&gt;0;                            //  Loop over the input
    c+=                                  //   Append `c` with:
       a=                                //    Set `a` to:
         ((--b+&quot;&quot;).indexOf(55)&amp;b&amp;7)      //     If `b` is divisible by or contains 7:
                                         //     (and decrease `b` by 1 in the process)
                                   &gt;&gt;31  //      Bitwise right shift so `a` is either `-1` or `0`
  );                                     //  End of loop
  return a&lt;0?                            //  If `a` is now -1:
    -b                                   //   Return the regular counter `b` (as positive)
   :                                     //  Else:
    c(c);                                //   Recursive call with 7-counter `c` as input
}                                        // End of method