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;
struct ANS {
  char str[5000010];
  int len;
  ANS() { len = 0; }
  void extend(char* s, int l, bool rev = false) {
    if (!rev) {
      for (int i = 0; i < l; i++) str[++len] = s[i];
    } else {
      for (int i = l - 1; i >= 0; i--) str[++len] = s[i];
    }
    return;
  }
  void solve(char* s, int l) {
    for (int i = 0; i < l; i++)
      if (s[i] < s[l - i - 1]) {
        extend(s, l, false);
        return;
      } else if (s[i] > s[l - i - 1]) {
        extend(s, l, true);
        return;
      }
    extend(s, l);
    return;
  }
  bool operator<(const ANS& x) const {
    for (int i = 1; i <= len; i++)
      if (str[i] < x.str[i])
        return true;
      else if (str[i] > x.str[i])
        return false;
    return false;
  }
} ans, tmp1, tmp2, tmp3;
char s[10000010];
int n;
int z[10000010];
void initZ(int len) {
  z[1] = len;
  for (int i = 2, j = 1, k = 1; i <= len; i = k) {
    if (j < i) j = i;
    while (j <= len && s[j] == s[j - i + 1]) j++;
    z[i] = j - i;
    k = i + 1;
    while (k + z[k - i + 1] < j) {
      z[k] = z[k - i + 1];
      k++;
    }
  }
  return;
}
int pos[10000010], l[10000010], cnt;
void lyndon() {
  int i = 1, j, k;
  while (i <= n) {
    j = i;
    k = i + 1;
    while (k <= n && s[k] >= s[j]) {
      if (s[k] > s[j])
        j = i;
      else
        j++;
      k++;
    }
    pos[cnt] = i;
    l[cnt] = k - j;
    cnt++;
    while (i <= j) i += k - j;
  }
  return;
}
void solve1(int N) {
  tmp1.extend(s + 1, N, true);
  return;
}
char T[10000010];
void solve2(int N) {
  for (int i = 1; i <= N; i++) T[i] = T[N + i] = s[i];
  int i = 1, j = 2, k = 0;
  while (i <= N && j <= N && k < N) {
    if (T[i + k] == T[j + k])
      k++;
    else if (T[i + k] < T[j + k]) {
      j = j + k + 1;
      k = 0;
    } else {
      i = i + k + 1;
      k = 0;
    }
    if (i == j) j++;
  }
  int p = min(i, j);
  tmp2.extend(T + p, N);
  return;
}
void solve3(int N) {
  int res = N;
  for (int i = N - 1; i >= 0; i--) {
    int p = z[n * 2 - res + 2];
    if (p < res - i) {
      if (s[res - p] < s[p + 1]) res = i;
    } else {
      p = z[res - i + 1];
      if (s[p + 1] < s[res - i + p + 1]) res = i;
    }
  }
  tmp3.extend(s + res + 1, pos[cnt + 1] - res - 1, true);
  tmp3.extend(s + 1, res);
  return;
}
void solve4(int N) {
  int res = cnt;
  while (2 * (pos[res + 1] - pos[res]) <= (pos[res] - pos[res - 1])) {
    bool flag = false;
    for (int i = pos[res] - 1; i >= pos[res - 1]; i--) {
      int x = N - (i - pos[res - 1]);
      if (s[x] != s[i]) {
        flag = s[i] < s[x];
        break;
      }
    }
    if (flag) break;
    res--;
  }
  ans.extend(s + pos[res], N - pos[res] + 1);
  ans.extend(s + 1, pos[res] - 1, true);
  return;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  reverse(s + 1, s + n + 1);
  for (int i = 1; i <= n; i++) s[2 * n - i + 2] = s[i];
  s[n + 1] = '#';
  initZ(2 * n + 1);
  lyndon();
  pos[cnt] = n + 1;
  cnt--;
  int k;
  scanf("%d", &k);
  while (k > 2 && cnt >= 0) {
    if (l[cnt] == 1) {
      while (cnt && l[cnt] == 1) {
        ans.extend(s + pos[cnt], pos[cnt + 1] - pos[cnt]);
        cnt--;
      }
    } else {
      ans.extend(s + pos[cnt], pos[cnt + 1] - pos[cnt]);
      cnt--;
    }
    k--;
  }
  if (cnt < 0) {
    printf("%s\n", ans.str + 1);
    return 0;
  }
  if (k == 1)
    ans.solve(s + 1, pos[cnt + 1] - 1);
  else {
    tmp1 = tmp2 = tmp3 = ans;
    int N = pos[cnt + 1] - 1;
    solve1(N);
    solve2(N);
    solve3(N);
    solve4(N);
    if (tmp1 < ans) ans = tmp1;
    if (tmp2 < ans) ans = tmp2;
    if (tmp3 < ans) ans = tmp3;
  }
  printf("%s\n", ans.str + 1);
  return 0;
}