/*************************************************
Author :supermaker
Created Time :2015/12/4 23:03:38
File Location :C:\Users\abcd\Desktop\TheEternalPoet
**************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <set>
#include <bitset>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <complex>
#include <numeric>
#include <functional>
#include <cmath>
#include <ctime>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
using namespace std;
#define CFF freopen ("aaa.txt", "r", stdin)
#define CPPFF ifstream cin ("aaa.txt")
#define DB(ccc) cout << #ccc << " = " << ccc << endl
#define PB push_back
#define MP(A, B) make_pair(A, B)
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair <int, int> PII;
typedef pair <int, bool> PIB;
const int INF = 0x7F7F7F7F;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = 2 * acos (0.0);
const int maxn = 1e5 + 6666;
LL dp[166][16][3], bit[166];
LL SS (int pos, int mod, int st, bool flag)
{
if (pos == 0) return (st == 2 && mod == 0);
if (flag && dp[pos][mod][st] != -1) return dp[pos][mod][st];
LL res = 0;
int x = flag ? 9 : bit[pos];
for (int i = 0; i <= x; i++)
{
int tt = (mod * 10 + i) % 13;
if ((st == 2) || (st == 1 && i == 3)) res += SS (pos - 1, tt, 2, flag || i < x);
else if (i == 1) res += SS (pos - 1, tt, 1, flag || i < x);
else res += SS (pos - 1, tt, 0, flag || i < x);
}
return flag ? dp[pos][mod][st] = res : res;
}
LL Calc (LL val)
{
memset (dp, -1, sizeof (dp));
int len = 0;
while (val)
{
bit[++len] = val % 10;
val /= 10;
}
return SS (len, 0, 0, 0);
}
int main()
{
//CFF;
//CPPFF;
LL n;
while (scanf ("%I64d", &n) != EOF)
{
printf ("%I64d\n", Calc (n));
}
return 0;
}