function main(input){
    lines = input.split(" ");

    var N = lines[0];
    var X = lines[1];
    var l="131";

    if(N==0){
        l="1";
    }else if(N==1){
        l="131";
    }else{
        for(var i=0;i<N-1;i++){
            var r = l.charAt(0);
            l = l.slice(1);
            l = l.slice(0,-1);
            var t =1 + Number(r);
            var b = "" + t + l + r + 1 + r + l +t;
            l=b;
        }
    }

    var enco=0;
    var ans=0;
    var sub=0;
    for(var i=0;i<X;i++){
        enco+=Number(l.charAt(i));
        if(enco>X){
            break;
        }
        i++;
        sub=Number(l.charAt(i));
        enco += sub;
        ans += sub;
        if(enco>=X){
            var u = enco - X;
            ans-=u;
            break;
        }
    }

    console.log(ans);

}

main(require('fs').readFileSync('/dev/stdin','utf8'));