description: Let's call the string beautiful if it does not contain a substring of length at least 2, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.
  
  Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 3 letters of the Latin alphabet (in lowercase).
  
  You are given a string s of length n, each character of the string is one of the first 3 letters of the Latin alphabet (in lowercase).
  
  You have to answer m queries — calculate the cost of the substring of the string s from l_i-th to r_i-th position, inclusive.
  
  Input
  
  The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of the string s and the number of queries.
  
  The second line contains the string s, it consists of n characters, each character one of the first 3 Latin letters.
  
  The following m lines contain two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — parameters of the i-th query.
  
  Output
  
  For each query, print a single integer — the cost of the substring of the string s from l_i-th to r_i-th position, inclusive.
  
  Example
  
  Input
  
  
  5 4
  baacb
  1 3
  1 5
  4 5
  2 3
  
  
  Output
  
  
  1
  2
  0
  1
  
  Note
  
  Consider the queries of the example test.
  
    * in the first query, the substring is baa, which can be changed to bac in one operation; 
    * in the second query, the substring is baacb, which can be changed to cbacb in two operations; 
    * in the third query, the substring is cb, which can be left unchanged; 
    * in the fourth query, the substring is aa, which can be changed to ba in one operation. 
solution: import sys
  #from collections import deque #Counter
  #sys.setrecursionlimit(20000)
  #import itertools
  from itertools import accumulate
  
  
  def rall():
      return sys.stdin.readlines()
  def rl():
      return sys.stdin.readline().strip()
  def rl_types(types):
      str_list = [x for x in sys.stdin.readline().strip().split(' ')]
      return [types[i](str_list[i]) for i in range(len(str_list))]
  
  def pr( something ):
      sys.stdout.write( str(something) + '\n')
  def pra( array ):
      sys.stdout.write( ' '.join([str(x) for x in array]) + '\n')
  
  
  def solve(array):
      return array
  
  
  if __name__ == '__main__':
  
      #NT = int( rl() )
      n,m = [int(x) for x in rl().split(' ')]
      s = rl()
      VALID_SHORTS = set( ['a','b','c','ab','ac','ba','bc','ca','cb', \
                           'abc','bca','cab','acb','bac','cba'])
      COSTS_SHORTS = {'aa':1,'bb':1,'cc':1,'aaa':2,'bbb':2,'ccc':2}
  
      # KEY: a "rotation" of a,b,c is the only way to have no palindromes
      # 2 possible orders(abc,acb). But, depending on which letter start with, 6 possible sequences
  
      # let's flag all the characters that don't fit each given sequence and build cum tables so that we can, for each query range, know what the number of problem letters is (and return the min of those over the 6 sequences).  We may need to handle the ends differently, but I'm not sure
      #pr(s)
      SEQS = ['abc','bca','cab','acb','bac','cba']
      CONFLICTS = {}
      for seq in SEQS:
          conflict = [0 if c==seq[i%3] else 1 for i,c in enumerate(s)]
          #print(seq,conflict)
          CONFLICTS[seq] = list(accumulate([0,*conflict]))
      #CONFLICTS = {seq:[0]*n for seq in SEQS}
      #pr(CONFLICTS)
  
      for _ in range(m):
          #l,r = [int(x) for x in rl().split(' ')]
          l,r = map(int,rl().split(' '))#[int(x) for x in rl().split(' ')]
          # vals = rl_types( [str,float,float] )
          #res = solve(array)
  
  
          nchar = r-l+1
  
          #pr(f'{l} {r} {subs}')
  
          if nchar <= 3: # short sequence
              subs = s[(l-1):r]
              if subs in VALID_SHORTS: # this handles all 1-2 char strings except 'aa' etc
                  cost = 0
              elif subs in COSTS_SHORTS: # this handles 'aa' and 'aaa' etc
                  cost = nchar-1
              #elif len(set(subs))==1: # this handles 'aa' and 'aaa' etc
                  #cost = nchar-1
              else: # must be something like aab
                  cost = 1
          else: # longer sequence
              cost = min( [CONFLICTS[seq][r]-CONFLICTS[seq][l-1] for seq in SEQS] )
              #cost = float('inf')
              #for seq in SEQS:
                  #conf = CONFLICTS[seq]
                  #cost = min(cost, CONFLICTS[seq][r] - CONFLICTS[seq][l-1])
          pr(cost)
  