N-order markov chain implementation