#include #if __has_include() #endif using namespace std; #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll IJK=n;IJK-->0;) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);i(1,__VA_ARGS__);i>=i##stop;i+=i##step) #define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a) #define defpp templatevoid pp(const auto&...a){[[maybe_unused]]const char*c="";((o<(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} templateconstexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} auto encode_integer(char c){return c-'0';} constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;} templateconstexpr ll maxsize10(){return size10(numeric_limits::max());} bool amin(auto&a,const auto&b){return b>{a...});} templatestruct pair{ A a;B b; pair()=default; pair(A aa,B bb):a(aa),b(bb){} auto operator<=>(const pair&)const=default; }; templatestruct infinity{ templatestatic constexpr T ones(size_t n){return n?ones(n-1)*10+1:0;} templateconstexpr operator T()const{static constexpr T v=ones(maxsize10())*(1-is_negative*2);return v;} templateconstexpr bool operator==(const T&x)const{return static_cast(*this)==x;} }; constexpr infinity oo; templateusing pack_back_t=tuple_element_t>; } namespace my{ templateconcept vectorial=is_base_of_v::value_type>,remove_cvref_t>; templateconstexpr int depth=0; templateconstexpr int depth =depth+1; templatestruct core_t_helper{using type=T;}; templatestruct core_t_helper{using type=typename core_t_helper::type;}; templateusing core_t=core_t_helper::type; templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ static constexpr int D=depth>; using C=core_t; using vector::vector; vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);} void resizes(const auto&...a){if constexpr(sizeof...(a)==D){ }else*this=make(a...);} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec(n,array{a...}[0]); else return vec(n,make(a...)); } ll size()const{return vector::size();} auto&emplace_back(auto&&...a){vector::emplace_back(std::forward(a)...);return*this;} auto fold(const auto&f)const{ pairr{}; fe(*this,e){ if constexpr(!vectorial){ if(r.b)f(r.a,e); else r={e,1}; }else { } } return r; } auto max()const{return fold([](auto&a,auto b){if(a=0); if(M==-1)M=max(); ll n=size(); vec>res(n+1,M+1,-1); fo(i,1,n+1){ fo(x,M+1)res[i][x]=res[i-1][x]; res[i][(*this)[i-1]]=i-1; } return res; } }; templaterequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; auto sin(){string s;lin(s);return s;} templateauto sinen_integer(){vecr;fe(sin(),e)r.eb(encode_integer(e));return r;} } namespace my{ auto decimal(ll x,ll L=-1){if(L==-1)L=size10(x);vecr;while(x)r.eb(x%10),x/=10;r.resize(L);return r;} } namespace my{entry void main(){ LL(N,Q); auto s=sinen_integer(); vec>v; fo(i,1000)if(i%8==0)v.eb(decimal(i,3).rev()); ll A=10; auto pre=s.pre_groups(A); FO(Q){ LL(l,r);--l; if(r-l==1){ pp(s[l]%8==0?0:-1); continue; }else if(r-l==2){ if((s[l]*10+s[l+1])%8==0){ pp(0); continue; }else if((s[l+1]*10+s[l])%8==0){ pp(1); continue; }else{ pp(-1); continue; } } ll res=oo; fe(v,tar){ veccnt(A); vecpos(3,-1); of(i,3){ ll cu=pre[r][tar[i]]; FO(cnt[tar[i]]){ if(cu==-1)break; cu=pre[cu][tar[i]]; } pos[i]=cu; cnt[tar[i]]++; } if(pos.min()