Problem:
﻿Score: 300 points

Problem Statement

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.
You are interested in what the string looks like after 5 \times 10^{15} days. What is the K-th character from the left in the string after 5 \times 10^{15} days?

Constraints

S is a string of length between 1 and 100 (inclusive).

K is an integer between 1 and 10^{18} (inclusive).

The length of the string after 5 \times 10^{15} days is at least K.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5 \times 10^{15} days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

The first five characters in the string after 5 \times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

Sample Input 2

3
157

Sample Output 2

3

The initial string is 3. The string after 5 \times 10^{15} days consists only of 3.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused)]

use std::{cell, cmp, collections, fmt, ops};

use cell::{Cell, RefCell};
use cmp::Ordering::{Equal, Greater, Less};
use collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use fmt::{Debug, Display};

#[cfg(feature = "debug")]
macro_rules! debug {
    ($($args: tt)*) => { eprint!($($args)*) };
}

#[cfg(feature = "debug")]
macro_rules! debugln {
    ($($args: tt)*) => { eprintln!($($args)*) };
}

#[cfg(not(feature = "debug"))]
macro_rules! debug {
    ($($args:tt)*) => {};
}

#[cfg(not(feature = "debug"))]
macro_rules! debugln {
    ($($args:tt)*) => {};
}

macro_rules! vmin {
    ($x: expr, $y: expr) => { cmp::min($x, $y) };
    ($x: expr, $($args: expr),*) => { cmp::min($x, vmin!($($args),*)) };
}

macro_rules! vmax {
    ($x: expr, $y: expr) => { cmp::max($x, $y) };
    ($x: expr, $($args: expr),*) => { cmp::max($x, vmax!($($args),*)) };
}

const INFINITY: u64 = 5000_0000_0000_0000;

fn main() {
    use io::*;

    let s = read_line();
    let k = parse_line::<u64>();

    let s_digits = s.chars().filter_map(|d| d.to_digit(10)).collect::<Vec<_>>();

    let mut ans = 0;
    for (i, &d) in s_digits.iter().enumerate() {
        if d != 1 || i + 1 == k as usize {
            ans = d;
            break;
        }
    }

    println!("{}", ans);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        //
    }
}

mod io {
    use std::io::{self, Read, Write};
    use std::str;

    pub fn read_line() -> String {
        let mut buf = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        buf.trim().to_owned()
    }

    pub fn read_word() -> String {
        const CAPACITY: usize = 16;

        let mut stdin = io::stdin();
        let mut byte: [u8; 1] = [0];

        loop {
            let mut buf = Vec::with_capacity(CAPACITY);

            loop {
                let res = stdin.read(&mut byte);

                if res.unwrap_or(0) == 0 || byte[0] <= b' ' {
                    break;
                } else {
                    buf.push(byte[0]);
                }
            }

            if !buf.is_empty() {
                return String::from_utf8(buf).unwrap();
            }
        }
    }

    pub fn parse_line<T: str::FromStr>() -> T {
        read_line().parse().ok().unwrap()
    }

    pub fn parse_word<T: str::FromStr>() -> T {
        read_word().parse().ok().unwrap()
    }

    pub fn parse_vec<T: str::FromStr>() -> Vec<T> {
        read_line()
            .split_whitespace()
            .map(|e| e.parse().ok().unwrap())
            .collect()
    }

    pub fn flush() {
        io::stdout().flush();
    }
}

mod num {
    use std::{cmp, ops};

    pub fn imin<T, I>(v: &T) -> &I
    where
        T: ops::Deref<Target = [I]>,
        I: cmp::Ord + Copy,
    {
        v.iter().min().unwrap()
    }

    pub fn imax<T, I>(v: &T) -> &I
    where
        T: ops::Deref<Target = [I]>,
        I: cmp::Ord + Copy,
    {
        v.iter().max().unwrap()
    }

    pub fn next_prime(x: i32) -> i32 {
        for i in (x + 1).. {
            if is_prime(i) {
                return i;
            }
        }

        panic!("attempt to add with overflow");
    }

    pub fn is_prime(x: i32) -> bool {
        let ceil = f64::from(x).sqrt().ceil() as i32;
        x == 2 || (2..(ceil + 1)).all(|i| x % i != 0)
    }
}

mod bits {
    pub fn bit_high(bits: u64, pos: u64) -> bool {
        bits & (1 << pos) != 0
    }
}