Imports System.Collections.Generic
Imports System.Console

Module Program
    Dim n, ans As Integer

    Sub Main()
        n = NextInt()
        For i As Integer = 0 To Integer.MaxValue
            DFS(i, "0")
        Next
    End Sub

    Sub DFS(rest As Integer, s As String)
        If rest = 0 Then
            If CLng(s) > n Then
                WriteLine(ans)
                End 'Program'
            End If
            For Each v As Char In "357"
                If Not s.Contains(v) Then
                    Return
                End If
            Next
            ans += 1
        Else
            For Each c As Char In "357"
                DFS(rest - 1, s & c)
            Next
        End If
    End Sub

    Function NextInt() As Integer
        Return CInt(NextString())
    End Function

    Function NextString() As String
        Dim result As New List(Of Char)
        Do
            Dim tmp As Integer = Read()
            If tmp < 0 Then Exit Do
            Dim nextChar As Char = Chr(tmp)

            If Not Char.IsWhiteSpace(nextChar) Then
                result.Add(nextChar)
            ElseIf nextChar <> vbCr Then
                Exit Do
            End If
        Loop
        Return New String(result.ToArray())
    End Function
End Module
