#include <bits/stdc++.h>
using namespace std;
long pow(int x, int n) {
  long ans = 1;
  for (int i = 0; i < n; i++) {
    ans *= x;
  }
  return ans;
}
long sg(int n, int k) {
  switch (k) {
    case 0:
      return pow(3, n) - 3 * pow(2, n) + 3;
      break;
    case 1:
      return pow(3, n) - 2 * pow(2, n) + 1;
      break;
    case 2:
      return pow(3, n) - pow(2, n);
      break;
    case 3:
      return pow(3, n);
      break;
    default:
      return 0;
      break;
  }
}
long mketa(long N) {
  for (int i = 9; i > 0; i--) {
    if (pow(10, i) > N && pow(10, i - 1) < N) {
      return i;
      break;
    }
  }
}
long solve(long N, int detaka[]) {
  detaka[3] = detaka[1] + detaka[2] + detaka[0];
  int nn = mketa(N);
  long ans = 0;
  if (nn == 1) {
    switch (detaka[3]) {
      case 0:
      case 1:
        ans = 0;
        break;
      case 2:
        ans = 1;
        break;
      case 3:
        ans = 3;
        break;
    }
  } else {
    for (int i = 1; i < nn; i++) {
      if (detaka[3] == 0) {
        ans += sg(i, detaka[3]);
      }
    }
    int NMI = N / pow(10, nn - 1);
    for (int i = 0; i <= NMI; i++) {
      if (i == NMI && (i == 3 || i == 5 || i == 7)) {
        detaka[(i - 3) / 2] = 1;
        ans += solve(N % pow(10, nn - 1), detaka);
      } else if (i == 3 || i == 5 || i == 7) {
        if (detaka[(i - 3) / 2] == 0) {
          ans += sg(nn - 1, detaka[3] + 1);
        } else {
          ans += sg(nn - 1, detaka[3]);
        }
      }
    }
  }
  return ans;
}
int main() {
  long N;
  cin >> N;
  int detaka[4] = {0, 0, 0, 0};
  cout << solve(N, detaka) << endl;
}
