using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("31");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("7");
//1
}
else if (InputPattern == "Input3") {
WillReturn.Add("111");
//8
}
else if (InputPattern == "Input4") {
WillReturn.Add("777777777777777777");
//271402266318408
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
static void Main()
{
List<string> InputList = GetInputList();
string StrN = InputList[0];
// 場合の数[7を法とした余り,数字自由フラグ,0以外登場有無]なDP表
long[, ,] PrevDP = new long[7, 2, 2];
PrevDP[0, 0, 0] = 1;
// 重み[Ind]なDict
var OmomiDict = new Dictionary<long, long>();
long Omomi = 1;
for (long I = StrN.Length - 1; 0 <= I; I--) {
OmomiDict[I] = Omomi;
Omomi *= 10;
Omomi %= 7;
}
for (long I = 0; I <= StrN.Length - 1; I++) {
long[, ,] CurrDP = new long[7, 2, 2];
for (long J = 0; J <= PrevDP.GetUpperBound(0); J++) {
for (long K = 0; K <= 1; K++) {
for (long L = 0; L <= 1; L++) {
if (PrevDP[J, K, L] == 0) continue;
for (char NewChar = '0'; NewChar <= '7'; NewChar++) {
if (K == 0 && StrN[(int)I] < NewChar) break;
long NewJ = J + OmomiDict[I] * (NewChar - '0');
NewJ %= 7;
long NewK = K;
if (StrN[(int)I] > NewChar) NewK = 1;
if (L == 1 && NewChar == '0') continue;
long NewL = L;
if (NewChar != '0' && L == 0) {
NewL = 1;
}
CurrDP[NewJ, NewK, NewL] += PrevDP[J, K, L];
}
}
}
}
PrevDP = CurrDP;
}
long Answer = 0;
Answer += PrevDP[0, 0, 1];
Answer += PrevDP[0, 1, 1];
Console.WriteLine(Answer);
}
}