#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;
}
