def backtracking(depth): global ans if depth == m: print(*ans) return for i in range(1, n+1) ans.append(i) backtracking(depth+1) ans.pop()