It is an interesting exercise to write a program to print out all permutations of $1, 2, …, n$. However, since there are $6227020800$ permutations of $1, 2, …, 13$, it is unlikely that we would ever run this program on an input of size more than $10$.
However, here is another interesting problem whose solution can also be used to generate permutations. We can order the permutations of $1, 2, …, n$ under the lexicographic (or dictionary) order. Here are the permutations of $1,2,3$ in lexicographic order:
123132213231312321123132213231312321 1 \, 2 \, 3 \quad    1 \, 3 \, 2 \quad     2 \, 1 \, 3 \quad     2 \, 3 \, 1 \quad     3 \, 1 \, 2 \quad     3 \, 2 \, 1 
The problem we have is the following: given a permutation of $1,2, …, n$, generate the next permutation in lexicographic order. For example, for $2 3 1 4$ the answer is $2 3 4 1$.

-----Input:-----
The first line of the input contains two integers, $N$ and $K$. This is followed by $K$ lines, each of which contains one permutation of $1, 2,…,N$.

-----Output:-----
The output should consist of $K$ lines. Line $i$ should contain the lexicographically next permutation correponding to the permutation on line $i+1$ in the input.

-----Constraints:-----
- $1 \leq N \leq 1000$.
- $1 \leq K \leq 10$.

-----Sample input-----
3 2
3 1 2
2 3 1

-----Sample output-----
3 2 1
3 1 2

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'class Solution(object):\r\n   def nextPermutation(self, nums):\r\n      found = False\r\n      i = len(nums)-2\r\n      while i >=0:\r\n         if nums[i] < nums[i+1]:\r\n            found =True\r\n            break\r\n         i-=1\r\n      if not found:\r\n         nums.sort()\r\n      else:\r\n         m = self.findMaxIndex(i+1,nums,nums[i])\r\n         nums[i],nums[m] = nums[m],nums[i]\r\n         nums[i+1:] = nums[i+1:][::-1]\r\n      return nums\r\n   def findMaxIndex(self,index,a,curr):\r\n      ans = -1\r\n      index = 0\r\n      for i in range(index,len(a)):\r\n         if a[i]>curr:\r\n            if ans == -1:\r\n               ans = curr\r\n               index = i\r\n            else:\r\n               ans = min(ans,a[i])\r\n               index = i\r\n      return index\r\nob1 = Solution()\r\nn,k=map(int,input().split())\r\nl=[]\r\nfor i in range(k):\r\n    t=list(map(int,input().split()))\r\n    l.append(t)\r\nfor i in l:\r\n    print(*ob1.nextPermutation(i))\r\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 16413