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;
const int MAX_N = 5e6 + 10;
int N, K, min_suf_len[MAX_N], zSr[MAX_N], match[MAX_N];
char S[MAX_N], Sr[MAX_N], Sres[MAX_N];
void init() {
  scanf("%s", S + 1);
  N = strlen(S + 1);
  scanf("%d", &K);
  for (int i = 1; i <= N; i++) Sr[i] = S[N - i + 1];
  for (int i = 1, j, k; i <= N;) {
    min_suf_len[i] = 1;
    for (j = i, k = i + 1; k <= N && Sr[k] >= Sr[j]; k++) {
      if (Sr[k] == Sr[j]) {
        min_suf_len[k] = min_suf_len[j];
        j++;
      } else {
        min_suf_len[k] = k - i + 1;
        j = i;
      }
    }
    while (i + k - j <= k) i += k - j;
  }
}
void exkmp() {
  int p = 1;
  for (int i = 2; i <= N; i++) {
    zSr[i] = max(min(zSr[i - p + 1], p + zSr[p] - i), 0);
    while (i + zSr[i] - 1 <= N && Sr[i + zSr[i]] == Sr[1 + zSr[i]]) zSr[i]++;
    if (i + zSr[i] > p + zSr[p]) p = i;
  }
  p = 1;
  for (int i = 1; i <= N; i++) {
    match[i] = max(min(p + match[p] - i, zSr[i - p + 1]), 0);
    while (i + match[i] <= N && S[i + match[i]] == Sr[1 + match[i]]) match[i]++;
    if (i + match[i] > p + match[p]) p = i;
  }
}
void check_rev_front(int curr) {
  int i, j, k;
  for (i = N; i >= curr;) {
    for (j = i, k = i - 1; k >= curr && S[k] >= S[j]; k--)
      S[k] == S[j] ? j-- : j = i;
    if (k < curr) {
      int len = j - k;
      for (j = i; j - len >= curr; j -= len)
        ;
      break;
    }
    while (i - (j - k) >= k) i -= (j - k);
  }
  static char s1[MAX_N], s2[MAX_N];
  for (k = 1; k <= N - curr + 1; k++) {
    s1[k] = k <= j - curr + 1 ? S[j - k + 1] : S[curr + k - 1];
    s2[k] = k <= i - curr + 1 ? S[i - k + 1] : S[curr + k - 1];
  }
  if (strcmp(s1 + 1, Sres + curr) < 0) strcpy(Sres + curr, s1 + 1);
  if (strcmp(s2 + 1, Sres + curr) < 0) strcpy(Sres + curr, s2 + 1);
  reverse(s1 + j - curr + 2, s1 + N - curr + 2);
  reverse(s2 + i - curr + 2, s2 + N - curr + 2);
  if (strcmp(s1 + 1, Sres + curr) < 0) strcpy(Sres + curr, s1 + 1);
  if (strcmp(s2 + 1, Sres + curr) < 0) strcpy(Sres + curr, s2 + 1);
}
void solve() {
  int curr = 1;
  bool kept = false;
  while (curr <= N && K > 2) {
    int len = min_suf_len[N - curr + 1];
    if (len == 1) {
      Sres[curr] = S[curr];
      curr++;
      kept = true;
      continue;
    }
    if (kept) K--, kept = false;
    int t = curr + len;
    while (t <= N && S[t] == S[t - len]) t++;
    while (t % len != curr % len) t--;
    for (int i = 1; i <= t - curr; i++) Sres[t - i] = S[curr + i - 1];
    K--;
    curr = t;
  }
  if (K == 1) {
    bool flag = false;
    for (int i = 1; i <= N - curr + 1; i++) {
      if (S[curr + i - 1] != S[N - i + 1]) {
        flag = S[curr + i - 1] > S[N - i + 1];
        break;
      }
    }
    if (flag) {
      for (int i = 1; i <= N - curr + 1; i++) Sres[curr + i - 1] = S[N - i + 1];
    } else {
      for (int i = 1; i <= N - curr + 1; i++)
        Sres[curr + i - 1] = S[curr + i - 1];
    }
  } else {
    exkmp();
    int opt = curr;
    for (int i = curr + 1; i <= N; i++) {
      if (match[opt] >= i - opt) {
        int len = zSr[1 + i - opt];
        if (i + len <= N && Sr[1 + len] < Sr[i - opt + 1 + len]) opt = i;
      } else {
        int p = opt + match[opt];
        if (S[p] < Sr[1 + match[opt]]) opt = i;
      }
    }
    for (int i = 1; i <= opt - curr; i++) Sres[curr + i - 1] = S[curr + i - 1];
    for (int i = 1; i <= N - opt + 1; i++) Sres[opt + i - 1] = S[N - i + 1];
    check_rev_front(curr);
  }
  printf("%s\n", Sres + 1);
}
int main() {
  init();
  solve();
}