function k = permutation_distance_ulam ( a, b )

%*****************************************************************************80
%
%% permutation_distance_ulam() computes the Ulam distance of two permutations.
%
%  Discussion:
%
%    The permutations have a common length N, and are reorderings of the
%    integer 1 through N.
%
%    If we let L(P) be the length of the longest ascending subsequence 
%    of a permutation P, then the Ulam distance between A and B is
%
%      N - L ( A * inverse ( B ) ).
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    14 September 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer A(N), B(N), the permutations to be examined.
%
%  Output:
%
%    integer K, the Ulam metric distance between A and B.
%
  binv = permutation_inverse ( b );

  c = permutation_multiply ( a, binv );

  subseq = permutation_ascending_subsequence ( c );

  n = permutation_order ( a );
  sub_n = permutation_order ( subseq );

  k = n - sub_n;

  return
end
