package main import ( "bufio" "fmt" "io" "math" "os" "sort" "strconv" ) var iost *Iost type Iost struct { Scanner *bufio.Scanner Writer *bufio.Writer } func NewIost(fp io.Reader, wfp io.Writer) *Iost { const BufSize = 2000005 scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, BufSize), BufSize) return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)} } func (i *Iost) Text() string { if !i.Scanner.Scan() { panic("scan failed") } return i.Scanner.Text() } func (i *Iost) Atoi(s string) int { x, _ := strconv.Atoi(s); return x } func (i *Iost) GetNextInt() int { return i.Atoi(i.Text()) } func (i *Iost) Atoi64(s string) int64 { x, _ := strconv.ParseInt(s, 10, 64); return x } func (i *Iost) GetNextInt64() int64 { return i.Atoi64(i.Text()) } func (i *Iost) Atof64(s string) float64 { x, _ := strconv.ParseFloat(s, 64); return x } func (i *Iost) GetNextFloat64() float64 { return i.Atof64(i.Text()) } func (i *Iost) Print(x ...interface{}) { fmt.Fprint(i.Writer, x...) } func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) } func (i *Iost) Println(x ...interface{}) { fmt.Fprintln(i.Writer, x...) } func isLocal() bool { return os.Getenv("NICKEL") == "BACK" } func main() { fp := os.Stdin wfp := os.Stdout if isLocal() { fp, _ = os.Open(os.Getenv("WELL_EVERYBODY_LIES_TOO_MUCH")) } iost = NewIost(fp, wfp) defer func() { iost.Writer.Flush() }() solve() } func solve() { k := iost.GetNextInt() more := sort.Search(math.MaxInt64>>4, func(x int) bool { return k <= f(x) }) if f(more) == k { iost.Println(more) return } iost.Println(-1) } func f(k int) int { aa := make([]int, 0) for k > 0 { aa = append(aa, k%10) k /= 10 } l := 0 r := len(aa) - 1 for l < r { aa[l], aa[r] = aa[r], aa[l] l++ r-- } n := len(aa) dp := make([][2]int, n+1) cnt := make([][2]int, n+1) ans := 0 cnt[n][0] = 1 for i := n - 1; i >= 0; i-- { for j := 0; j < 2; j++ { for d := 0; d < 10; d++ { tj := 0 if aa[i]-j < d { tj = 1 } add := 0 switch d { case 0, 4, 6, 9: add++ case 8: add += 2 } dp[i][tj] += dp[i+1][j] + add*cnt[i+1][j] cnt[i][tj] += cnt[i+1][j] if d == 0 { continue } if i > 0 || tj == 0 { ans += dp[i+1][j] + add*cnt[i+1][j] } } } } return ans }