Problem: You are given a non-empty line s and an integer k. The following operation is performed with this line exactly once:

  * A line is split into at most k non-empty substrings, i.e. string s is represented as a concatenation of a set of strings s = t1 + t2 + ... + tm, 1 ≤ m ≤ k. 
  * Some of strings ti are replaced by strings tir, that is, their record from right to left. 
  * The lines are concatenated back in the same order, we get string s' = t'1t'2... t'm, where t'i equals ti or tir. 



Your task is to determine the lexicographically smallest string that could be the result of applying the given operation to the string s.

Input

The first line of the input contains string s (1 ≤ |s| ≤ 5 000 000), consisting of lowercase English letters. The second line contains integer k (1 ≤ k ≤ |s|) — the maximum number of parts in the partition.

Output

In the single line print the lexicographically minimum string s' which can be obtained as a result of performing the described operation. 

Examples

Input

aba
2


Output

aab


Input

aaaabacaba
2


Output

aaaaabacab


Input

bababa
1


Output

ababab


Input

abacabadabacaba
4


Output

aababacabacabad

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m = 0, K, L[10000012], len[10000012], cn = 0, nex[10000012], mi;
char sc[10000012], ans[10000012], ol[10000012], t1[10000012], t2[10000012],
    t3[10000012], t4[10000012];
inline int cmp(char *s1, char *s2, int len) {
  for (int i = 1; i <= len; i++)
    if (s1[i] != s2[i]) return s1[i] < s2[i];
  return 2;
}
inline void Add(char *s1, int l, int r) {
  for (int i = l; i <= r; i++) ans[++cn] = s1[i];
}
inline void GetFail() {
  nex[1] = n;
  int i, j = 0, p = 0, len;
  for (i = 2; i <= n; ++i) {
    if ((i > p) || (j <= 1)) {
      len = 0;
      while ((sc[i + len] == sc[1 + len]) && (i + len <= n)) ++len;
      nex[i] = len;
    } else {
      if (i + nex[i - j + 1] - 1 < p)
        nex[i] = nex[i - j + 1];
      else {
        len = p - i + 1;
        while ((sc[i + len] == sc[1 + len]) && (i + len <= n)) ++len;
        nex[i] = len;
      }
    }
    if (i + nex[i] - 1 > p) {
      p = i + nex[i] - 1;
      j = i;
    }
  }
}
int main() {
  scanf("%s", sc + 1);
  n = strlen(sc + 1);
  memcpy(ol + 1, sc + 1, n);
  reverse(sc + 1, sc + n + 1);
  scanf("%d", &K);
  int i, j, k, v, v1, res;
  for (i = 1; i <= n;) {
    j = i;
    k = i + 1;
    for (; (k <= n) && (sc[j] <= sc[k]); ++k)
      if (sc[j] == sc[k])
        ++j;
      else
        j = i;
    ++m;
    len[m] = k - j;
    L[m] = i;
    while (i <= j) i += len[m];
  }
  L[m + 1] = n + 1;
  if (K == 1) {
    if (cmp(sc, ol, n))
      printf("%s", sc + 1);
    else
      printf("%s", ol + 1);
    return 0;
  }
  while (m && (K >= 3)) {
    Add(sc, L[m], L[m + 1] - 1);
    if ((len[m] != 1) || (len[m - 1] != 1)) --K;
    --m;
  }
  if (!m) {
    printf("%s", ans + 1);
    return 0;
  }
  res = m;
  m = L[m + 1] - 1;
  n = m << 1;
  for (i = 1; i <= m; i++) sc[n - i + 1] = sc[i], t1[i] = sc[m - i + 1];
  sc[n | 1] = 0;
  GetFail();
  if (m > 1) {
    mi = 1;
    for (i = 2; i < m; i++)
      if ((v1 = nex[m + mi + 1]) >= i - mi) {
        if ((v = nex[i - mi + 1]) < m - i)
          mi = (sc[i - mi + 1 + v] < sc[v + 1]) ? mi : i;
      } else
        mi = (sc[m + mi + 1 + v1] < sc[1 + v1]) ? i : mi;
    for (i = 1; i <= mi; i++) t2[i] = t1[i];
    for (i = mi + 1; i <= m; i++) t2[i] = t1[m - (i - mi) + 1];
  }
  for (i = 1; i <= m; i++) sc[i + m] = sc[i];
  i = 1;
  j = 2;
  k = 0;
  while ((i <= m) && (j <= m) && (k < m)) {
    if (i == j) {
      ++j;
      continue;
    }
    if (sc[i + k] == sc[j + k])
      ++k;
    else {
      if (sc[i + k] < sc[j + k])
        j += k + 1;
      else
        i += k + 1;
      k = 0;
    }
  }
  v = min(i, j);
  for (i = 1; i <= m; i++) t3[i] = sc[i + v - 1];
  m = res;
  while ((m > 1) && ((L[m + 1] - L[m]) * 2 <= (L[m] - L[m - 1]))) {
    bool fl = 0;
    for (i = L[m - 1]; i < L[m]; i++) {
      if (sc[i] > sc[i + L[m] - L[m - 1]]) {
        fl = 1;
        break;
      }
      if (sc[i] < sc[i + L[m] - L[m - 1]]) break;
    }
    if (fl) break;
    --m;
  }
  for (i = L[m]; i < L[res + 1]; i++) t4[i - L[m] + 1] = sc[i];
  for (i = L[m] - 1; i >= 1; i--) t4[L[res + 1] - i] = sc[i];
  if ((m > 1) && cmp(t2, t1, L[res + 1] - 1))
    memcpy(t1 + 1, t2 + 1, L[res + 1] - 1);
  if (cmp(t3, t1, L[res + 1] - 1)) memcpy(t1 + 1, t3 + 1, L[res + 1] - 1);
  if (cmp(t4, t1, L[res + 1] - 1)) memcpy(t1 + 1, t4 + 1, L[res + 1] - 1);
  Add(t1, 1, L[res + 1] - 1);
  printf("%s", ans + 1);
  return 0;
}