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;
char s[10000005], t[5000005], t1[5000005], t2[5000005], t3[5000005],
    t4[5000005];
int n, k, m, nowl, z[10000005], A[5000005], l[5000005];
void extend(char* s, int l) {
  for (int i = 0; i < l; i++) t[++nowl] = s[i];
}
void solve1(int n) {
  for (int i = 1; i <= n; i++) t1[i] = s[n - i + 1];
}
void solve2(int n) {
  for (int i = n + 1; i <= 2 * n; i++) s[i] = s[i - n];
  int i = 1, j = 2, k = 0;
  while (i <= n && j <= n && k < n) {
    if (s[i + k] == s[j + k])
      k++;
    else if (s[i + k] < s[j + k])
      j += k + 1, k = 0;
    else
      i += k + 1, k = 0;
    if (i == j) j++;
  }
  for (int x = 0; x < n; x++) t2[x + 1] = s[min(i, j) + x];
}
void solve3(int n) {
  for (int i = n + 1; i <= 2 * n; i++) s[i] = s[n - (i - n) + 1];
  n *= 2;
  z[1] = n;
  int l = 1, r = 1;
  for (int i = 2; i <= n; i++) {
    if (r < i) {
      l = r = i;
      while (r <= n && s[r] == s[r - l + 1]) r++;
      z[i] = r - l;
      r--;
    } else {
      if (z[i - l + 1] < r - i + 1)
        z[i] = z[i - l + 1];
      else {
        l = i;
        while (r <= n && s[r] == s[r - l + 1]) r++;
        z[i] = r - l;
        r--;
      }
    }
  }
  n /= 2;
  int j = n + 1;
  for (int i = n; i >= 1; i--) {
    int l = i, r = j - 1;
    l = n - l + 1, r = n - r + 1;
    int len = z[r + n];
    if (len < l - r + 1) {
      if (s[len + 1] > s[j - len - 1]) j = i;
    } else {
      len = j - i;
      l = len + 1, r = j - 1;
      if (s[z[l] + 1] < s[l + z[l]]) j = i;
    }
  }
  int tot = 0;
  for (int i = n; i >= j; i--) t3[++tot] = s[i];
  for (int i = 1; i < j; i++) t3[++tot] = s[i];
}
void solve4(int n) {
  int p = m;
  while ((A[p + 1] - A[p]) * 2 <= (A[p] - A[p - 1])) {
    bool flag = 0;
    for (int i = A[p] - 1; i >= A[p - 1]; i--) {
      if (s[i] < s[n - i + A[p - 1]]) {
        flag = 1;
        break;
      }
      if (s[i] > s[n - i + A[p - 1]]) break;
    }
    if (flag) break;
    p--;
  }
  p = A[p];
  int tot = 0;
  for (int i = p; i <= n; i++) t4[++tot] = s[i];
  for (int i = p - 1; i >= 1; i--) t4[++tot] = s[i];
}
void update(char* t1, char* t2, int n) {
  for (int i = 1; i <= n; i++) {
    if (t1[i] < t2[i]) return;
    if (t2[i] < t1[i]) break;
  }
  for (int i = 1; i <= n; i++) t1[i] = t2[i];
}
void solve(int n) {
  solve1(n);
  solve2(n);
  solve3(n);
  solve4(n);
  update(t1, t2, n);
  update(t1, t3, n);
  update(t1, t4, n);
  extend(t1 + 1, n);
}
int main() {
  scanf("%s", s + 1);
  n = (int)strlen(s + 1);
  scanf("%d", &k);
  for (int i = 1; i * 2 <= n; i++) swap(s[i], s[n - i + 1]);
  if (k == 1) {
    for (int i = 1; i <= n; i++) t[i] = s[n - i + 1];
    for (int i = 1; i <= n; i++) {
      if (s[i] < t[i]) {
        printf("%s\n", s + 1);
        return 0;
      }
      if (s[i] > t[i]) {
        printf("%s\n", t + 1);
        return 0;
      }
    }
    printf("%s\n", s + 1);
    return 0;
  }
  for (int i = 1; i <= n;) {
    int j = i, k = i + 1;
    while (s[j] <= s[k]) {
      if (s[j] < s[k])
        j = i;
      else
        j++;
      k++;
    }
    A[++m] = i;
    l[m] = k - j;
    while (i <= j) i += k - j;
  }
  A[m + 1] = n + 1;
  while (m && k >= 3) {
    extend(s + A[m], A[m + 1] - A[m]);
    if (l[m] != 1 || l[m - 1] != 1) k--;
    m--;
  }
  if (!m) {
    printf("%s\n", t + 1);
    return 0;
  }
  solve(A[m + 1] - 1);
  printf("%s\n", t + 1);
  return 0;
}