#include <bits/stdc++.h>
using namespace std;
long long n, m, ans;
long long a[300010], b[300010], pos[300010];
struct arr {
  long long sum, num;
  bool operator<(const arr &t) const { return sum > t.sum; }
};
priority_queue<arr> q1, q2, q3, q4, q5;
signed main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) scanf("%lld%lld", &a[i], &b[i]);
  for (long long i = 1; i <= n; i++) {
    pos[i] = 0;
    arr now;
    now.num = i;
    now.sum = a[i];
    q1.push(now);
    now.num = i;
    now.sum = b[i];
    q4.push(now);
  }
  for (long long i = 1; i <= m; i++) {
    long long sum1 = 1e18;
    while (!q1.empty()) {
      arr now;
      now = q1.top();
      if (pos[now.num] != 0) {
        q1.pop();
        continue;
      }
      sum1 = now.sum;
      break;
    }
    long long sum2 = 1e18;
    while (!q2.empty()) {
      arr now;
      now = q2.top();
      if (pos[now.num] != 1) {
        q2.pop();
        continue;
      }
      sum2 = now.sum;
      break;
    }
    long long tmp;
    while (!q4.empty()) {
      arr now;
      now = q4.top();
      if (pos[now.num] != 0) {
        q4.pop();
        continue;
      }
      tmp = now.sum;
      break;
    }
    long long sum3 = 1e18;
    long long sum4 = 1e18;
    if (!q4.empty()) {
      while (!q3.empty()) {
        arr now;
        now = q3.top();
        if (pos[now.num] != 2) {
          q3.pop();
          continue;
        }
        sum3 = now.sum + tmp;
        break;
      }
      while (!q5.empty()) {
        arr now;
        now = q5.top();
        if (pos[now.num] != 1) {
          q5.pop();
          continue;
        }
        sum4 = now.sum + tmp;
        break;
      }
    }
    long long key = 1;
    long long mn = sum1;
    if (mn > sum2) mn = sum2, key = 2;
    if (mn > sum3) mn = sum3, key = 3;
    if (mn > sum4) mn = sum4, key = 4;
    ans += mn;
    if (key == 1) {
      arr now;
      now = q1.top();
      q1.pop();
      pos[now.num] = 1;
      arr haha;
      haha.num = now.num;
      haha.sum = b[now.num] - a[now.num];
      q2.push(haha);
      haha.num = now.num;
      haha.sum = -a[now.num];
      q5.push(haha);
    }
    if (key == 2) {
      arr now;
      now = q2.top();
      q2.pop();
      pos[now.num] = 2;
      arr haha;
      haha.num = now.num;
      haha.sum = -b[now.num] + a[now.num];
      q3.push(haha);
    }
    if (key == 3) {
      arr now, haha;
      now = q3.top();
      q3.pop();
      pos[now.num] = 1;
      haha.num = now.num;
      haha.sum = -a[now.num];
      q5.push(haha);
      haha.num = now.num;
      haha.sum = b[now.num] - a[now.num];
      q2.push(haha);
      now = q4.top();
      q4.pop();
      pos[now.num] = 2;
      haha.num = now.num;
      haha.sum = -b[now.num] + a[now.num];
      q3.push(haha);
    }
    if (key == 4) {
      arr now, haha, now1;
      now = q5.top();
      q5.pop();
      pos[now.num] = 0;
      haha.num = now.num;
      haha.sum = a[now.num];
      q1.push(haha);
      now1 = q4.top();
      q4.pop();
      haha.num = now.num;
      haha.sum = b[now.num];
      q4.push(haha);
      pos[now1.num] = 2;
      haha.num = now1.num;
      haha.sum = -b[now1.num] + a[now1.num];
      q3.push(haha);
    }
  }
  printf("%lld\n", ans);
  for (long long i = 1; i <= n; i++) printf("%lld", pos[i]);
  return 0;
}
